#!/usr/bin/perl -w use strict; use DBI; ## need to have DBD::CSV installed for this script! my $dbh = DBI->connect ("DBI:CSV:", undef, undef, # no uname, pwd { f_dir => './', # default current directory f_ext => ".csv", # DB files end in .csv csv_eol => "\n", # default (used for writing) csv_sep_char => ',', # default of course is comma! RaiseError => 1, # errors are fatal (with description) PrintError => 0, # default is 0 not needed if RaiseError=1 # non-zero prints error and continues } ) or die "Cannot connect: " . $DBI::errstr; my $st_basic_actions = $dbh->prepare("SELECT action,userID,Place FROM DataBase ORDER BY userID"); $st_basic_actions->execute(); printf "%-10s %-10s %-10s\n", 'Action','userID','Place'; while (my ($action,$userID,$Place) = $st_basic_actions->fetchrow) { $Place ||= 'unknown'; # use 'unknown' if null string, '' printf "%-10s %-10s %-10s\n", $action,$userID,$Place } =outputs ################ Action userID Place G CHXAS unknown X LLXEAS Wah Woo, Section A X LLXEAS2 Wah Woo, Section A =cut ################ my $st = $dbh->prepare ("SELECT * FROM DataBase"); $st->execute(); while (my $href = $st->fetchrow_hashref()) { print "****\n"; foreach my $key (sort keys %$href) { print "$key=$href->{$key}\n" if $href->{$key}; } } =outputs ############### **** action=X dept2=DSA first_name=Lamshi footer=Welcome to Coo Duk, ltd. nameid=LLXEAS2 oxcode=A pc=HKA place=Wah Woo, Section A section=CWW state=Important status=NAW subject=No vell surname=Coo userid=LLXEAS2 **** action=X dept2=DSA first_name=Lamshi footer=Welcome to Coo Duk, ltd. nameid=LLXEAS oxcode=A pc=HKA place=Wah Woo, Section A section=CWW state=Important status=NAW subject=No vell surname=Coo userid=LLXEAS **** action=G second_mail_alias=sam.mo@hotmail.com userid=CHXAS =cut # **CODE UPDATE**: adding an "update" to show modification is possible $st = $dbh->prepare ("UPDATE DataBase SET action='Done' WHERE userID='LLXEAS2'"); my $rows_modified = $st->execute(); print "\nRows Modified from X to Done = $rows_modified\n"; ## now re-running previous query, can re-use a prepared statement ## handle $st_basic_actions->execute(); printf "%-10s %-10s %-10s\n", 'Action','userID','Place'; while (my ($action,$userID,$Place) = $st_basic_actions->fetchrow) { $Place ||= 'unknown'; # use 'unknown' if null string, '' printf "%-10s %-10s %-10s\n", $action,$userID,$Place } ########################## =this new section of code prints: Rows Modified from X to Done = 1 Action userID Place G CHXAS unknown X LLXEAS Wah Woo, Section A Done LLXEAS2 Wah Woo, Section A =cut ############################