Abend dedlfix
Perlcode:
#!/usr/bin/perl
# sollte in keinem Script fehlen
use strict;
use warnings;
# DBI Modul Laden
use DBI;
# Verzeichnis mit dem Script finden.
# "." muss nicht immer das Verzeichnis mit dem Script sein
use FindBin '$Bin';
# Sollte man machen.
# es hilft die Dateinamen
# von den Tabellennamen zu trennen.
my %zuordnung=(
prospects => {
file => 'prospects.csv',
col_names => ['Name', 'Address', 'Floors', 'Donated last year', 'Contact'],
},
);
# Verbindung aufbauen
my $dbh = DBI->connect("DBI:CSV:", undef, undef, {
# Zeilenseparator
csv_eol => "\x0A",
# Spaltenseparator:
csv_sep_char => ",",
# Verzeichnis mit den Dateien
f_dir => $Bin,
# Zuordnung Dateiname -> Tabelle
csv_tables => \%zuordnung,
# bei Fehlern immer sterben:
RaiseError => 1,
}
) or die($DBI::errstr);
# Testausgabe
my $sth=$dbh->prepare('SELECT * FROM prospects');
$sth->execute();
while(my @row = $sth->fetchrow_array){
print join('; ',@row)."\n";
}
$sth->finish;
my $row;
$sth = $dbh->prepare("SELECT * FROM prospects WHERE name LIKE 'G%'");
$sth->execute();
while (my $row = $sth->fetchrow_hashref){
print("name = $row->{Name} contact = $row->{Contact}"."\n");
}
$sth->finish();
$dbh->disconnect();
CSV-Inhalt (Dateiname: prospects.csv):
-----------------------------------------------------------------------------
"Name","Address","Floors","Donated last year","Contact"
"Charlotte French Cakes","1179 Glenhuntly Rd","1","Y","John"
"Glenhuntly Pharmacy","1181 Glenhuntly Rd","1","Y","Paul"
"Dick Wicks Magnetic Pain Relief","1183-1185 Glenhuntly Rd","1","Y","George"
"Gilmour's Shoes","1187 Glenhuntly Rd","1","Y","Ringo"
-----------------------------------------------------------------------------
Ich versuche mit diesem Übungscode eine DBD::CSV anzusteuern. Das gelingt nur teilweise. Die Ausgabe im Terminal sieht so aus:
-----------------------------------------------------------------------------
Name; Address; Floors; Donated last year; Contact
Charlotte French Cakes; 1179 Glenhuntly Rd; 1; Y; John
Glenhuntly Pharmacy; 1181 Glenhuntly Rd; 1; Y; Paul
Dick Wicks Magnetic Pain Relief; 1183-1185 Glenhuntly Rd; 1; Y; George
Gilmour's Shoes; 1187 Glenhuntly Rd; 1; Y; Ringo
Use of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 52.
Use of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 52.
name = contact =
Use of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 52.
Use of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 52.
name = contact =
-----------------------------------------------------------------------------
Irgendwo ist noch ein kleiner Fehler, den ich nicht finden kann.
Gruss HP-User