1 2 3
# Connect to the database, (the directory containing our csv file(s)) my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;");
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl use strict; use warnings; use DBI; # Connect to the database, (the directory containing our csv file(s)) my $dbh = DBI->connect("DBI:CSV:f_dir=../prospects.csv;csv_eol=\n;"); # Associate our csv file with the table name 'prospects' $dbh->{'csv_tables'}->{'prospects'} = { 'file' => 'prospects.csv'}; # Output the name and contact field from each row my $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();
QuoteUse of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 19.
Use of uninitialized value in print at ./DB-Test.pl line 19.
name = contact =
Use of uninitialized value in concatenation (.) or string at ./DB-Test.pl line 19.
Use of uninitialized value in print at ./DB-Test.pl line 19.
name = contact =
Quote"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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#!/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 => [qw( Name Contact Foo Bar Bam )], }, foo => { file => 'foo.csv', col_names => [qw( foo bar bam )], }, bar => { file => 'bar.csv', col_names => [qw( foo bar bam )], }, #... ); # 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 foo'); $sth->execute(); while(my @row = $sth->fetchrow_array) { print join('; ',@row)."\n"; } $sth->finish; } { my $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();
1 2 3 4 5 6 7 8 9 10 11
}, foo => { file => 'foo.csv', col_names => [qw( foo bar bam )], }, bar => { file => 'bar.csv', col_names => [qw( foo bar bam
1 2 3
# Verzeichnis mit dem Script finden. # "." muss nicht immer das Verzeichnis mit dem Script sein use FindBin '$Bin';
my $dbh = DBI->connect("dbi:CSV:", undef, undef,
2012-09-15T13:47:40 ?Hab dein Programm zum üben verwendet
2012-09-15T13:47:40 ?
Frage: Keine Trenner bei "Donated last year"? Seh ich das richtig, dass hier die Spaltenköpfe der CSV-Datei einzutragen sind?
1 2 3
col_names => [ 'Name', 'Address', 'Floors', 'Donated last year', 'Contact' ],
2012-09-15T13:47:40 ?Zeile 24-33 deines Codes: Schon wieder foo bar und bam. Sind das Platzhalter? Wenn ja für was?
2012-09-15T13:47:40 ?Zeile 10-12: Um das Verzeichnis mit dem zu Script finden. Das Perl-Programm, oder das DBI Programm? Oder Das DBD Programm? Was wird hier gesucht?
Code (perl): (dl )1 2 3# Verzeichnis mit dem Script finden. # "." muss nicht immer das Verzeichnis mit dem Script sein use FindBin '$Bin';
2012-09-15T13:47:40 ?Code (perl): (dl )my $dbh = DBI->connect("dbi:CSV:", undef, undef,
Warum steht hier ("dbi:CSV:") das DBI in kleinen Buchstaben? Ist das ein Fehler?
2012-09-15T13:47:40 ?Was bedeutet undef (klar undefiniert, aber was ist undefiniert?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/perl # sollte in keinem Script fehlen use strict; Usgabe auf der Console (Terminal) ist diese: [quote] use warnings; # DBI Modul Laden use DBI; # Verbindung aufbauen my $dbh = DBI->connect('DBI:CSV:f_dir=/home/ritter/Desktop'); # Testausgabe my $sth=$dbh->prepare('SELECT * FROM prospects.csv'); $sth->execute(); while(my @row = $sth->fetchrow_array) { print join(' ',@row)."\n"; $sth->finish; } $sth->finish(); $dbh->disconnect();
QuoteCharlotte French Cakes 1179 Glenhuntly Rd 1 Y John
DBD::CSV::st fetchrow_array failed: Attempt to fetch row without a preceeding execute () call or from a non-SELECT statement [for Statement "SELECT * FROM prospects.csv"] at ./DB-Test.pl line 16.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl # sollte in keinem Script fehlen use strict; use warnings; # DBI Modul Laden use DBI; # Verbindung aufbauen my $dbh = DBI->connect('DBI:CSV:f_dir=/home/ritter/Desktop'); # Testausgabe my $sth=$dbh->prepare('SELECT * FROM prospects.csv'); $sth->execute(); while(my @row = $sth->fetchrow_array) { print join(' ',@row)."\n"; } $sth->finish(); $dbh->disconnect();
QuoteCharlotte 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
Quote"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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/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; $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();
$sth = $dbh->prepare("SELECT * FROM prospects WHERE name LIKE 'G%'");
1 2 3
while (my $row = $sth->fetchrow_hashref){ print("name = $row->{Name} contact = $row->{Contact}". "\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Testausgabe ganze CSV Nr.1 my $sth=$dbh->prepare('SELECT * FROM prospects'); $sth->execute(); while(my @row = $sth->fetchrow_array){ print join('; ',@row)."\n"; } $sth->finish; #Textausgabe ganze CSV Nr.2 $sth = $dbh->prepare('SELECT * FROM prospects'); $sth->execute(); while (my $row = $sth->fetchrow_hashref){ print("name = $row->{Name} contact = $row->{Contact}\n"); } $sth->finish();
1
2
3
4
5
6
7
$VAR1 = {
'donated last year' => 'Y',
'floors' => '1',
'name' => 'Glenhuntly Pharmacy',
'contact' => 'Paul',
'address' => '1181 Glenhuntly Rd'
};
while (my $row = $sth->fetchrow_hashref)