#!perl use strict; use DBI; # set up database my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { csv_tables => { customer => { f_file => "customers.txt" , col_names => ['NAME','TELNO','EMAIL'], } }, csv_sep_char => " ", }) or die $DBI::errstr; print "Type 'q' to exit\n"; my @field = ('Name','TelNo','Email'); my $i=0; my $input; while (1) { do { # get input $i = 0 if $field[$i] eq ''; print $field[$i]."? "; chomp($input = ); ++$i; } until ($input) ; --$i; last if (lc $input eq 'q'); # query database my $fld = $field[$i]; print "Search for $fld contains '$input'\n"; my $sth = $dbh->prepare("SELECT * FROM customer WHERE $fld CLIKE ?"); # ignore case $sth->execute('%'.$input.'%'); my $count; while (my @f = $sth->fetchrow_array){ ++$count; print "$count $fld : $f[$i] -> $f[0] $f[1] $f[2]\n"; } print "Customer record not found. \n" unless ($count); $input=''; }