Schrift
[thread]3422[/thread]

Problem mit DBD::CSV: SELECT * FROM ... bring kein Ergebnis



<< >> 8 Einträge, 1 Seite
format_c
 2004-03-20 20:14
#31855 #31855
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Hi,
Ich möchte eine CSV-Datei mit DBI bearbeiten.

Habe Quelldatei in /home/format_c/tmp/csvdb :
mit folgendem Inhalt:
Code: (dl )
1
2
1,"0000000000000","Christine"
2,"+49000000000000","PS, Bernhardt"


Jetzt möchte ich einfach mal alle Einträge sehen damit ich weis dass DBD::CSV funktioniert. Aber irgendwie bin ich zu blöd.
Hier mein Code:
Code (perl): (dl )
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
#!/usr/bin/perl
use strict;
use warnings;
use DBI;

# Declare variables
my $db = '/home/format_c/tmp/csvdb/';
my $prefix = '+49';

# Makes a Datebase Handler
my $dbh = DBI->connect("DBI:CSV:f_dir=$db;csv_sep_char=,;")  or die DBI::errstr;
$dbh->{csv_tables}->{phonebook} = { file => 'Phonebook_SM.csv' };

my $sql = qq|
                                                SELECT * FROM phonebook
                                                |;
                                                
my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;                    
$sth->execute() or die $dbh->errstr;
while (my @row = $sth->fetchrow_array()) {
        print join "\t",@row . "\n";
}

$sth->finish();
$dbh->disconnect();

exit;


Ergibnis:
Code: (dl )
1
2
format_c@linux:~/Develop/Perl/Update Phonebook> perl set_germ_prefix.pl 
format_c@linux:~/Develop/Perl/Update Phonebook>


Kann mir jemand helfen?

Gruß Alex
format_c
 2004-03-21 17:51
#31856 #31856
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Niemand da der mal mit DBD::CSV gearbeitet hat?

Gruß Alex
Ronnie
 2004-03-21 18:14
#31857 #31857
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Bei mir geht es so:
Code: (dl )
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
#!/usr/bin/perl
use strict;
use warnings;
use DBI;

# Declare variables
my $db = 'C:/Perlen/csv';
my $prefix = '+49';

# Makes a Datebase Handler
my $dbh = DBI->connect("DBI:CSV:f_dir=$db")  or die DBI::errstr;
$dbh->{csv_tables}->{phonebook} = { file => 'test.csv' };

my $sql = qq|
SELECT * FROM phonebook
|;

my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my @row = $sth->fetchrow_array()) {
 print join "\t", @row;
 print "\n";
}

$sth->finish();
$dbh->disconnect();

exit;


Mit folgender CSV-Datei:
Code: (dl )
1
2
3
4
ID, Nummer, Vorname, Nachname
1,"0000000000000","Christine","Nachname"
2,"+49000000000000","PS","Bernhardt"
3,"+496985725","Irgendwer","Nachname"


Die erste Zeile wird anscheinend ignoriert.\n\n

<!--EDIT|Ronnie|1079885784-->
format_c
 2004-03-21 19:13
#31858 #31858
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Sehr sehr komisch. da passiert bei mir immer noch nix.
Ronnie
 2004-03-21 19:19
#31859 #31859
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Ich habe im Vergleich zu deinem Original, den Delimiter rausgenommen und in der while-Schleife die Ausgabe des newline in eine separate Zeile genommen, sowie die CSV-Datei ein wenig verändert - und es funktioniert. Leider habe ich keine Ahnung von DBD::CSV - wieso es die erste Zeile verwirft weiß ich nicht.

NACHTRAG:
Code: (dl )
1
2
3
4
5
$dbh->{csv_tables}->{phonebook} =
{
'file' => 'test.csv',
       'col_names' => ["ID", "Nummer", "Vorname", "Nachname"],
    };

Und schon geht es sogar zu 100%.\n\n

<!--EDIT|Ronnie|1079890092-->
format_c
 2004-03-21 19:27
#31860 #31860
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Die änderungen hab ich ja auch gemacht.
Ich probiers noch mal auf meiner Windows-Kiste mit dem Code.

Gruß Alex
Ronnie
 2004-03-21 19:28
#31861 #31861
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Die 'col_names' scheinen wichtig zu sein. Nochmal komplett:

Code: (dl )
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
#!/usr/bin/perl
use strict;
use warnings;
use DBI;

# Declare variables
my $db = 'C:/Perlen/csv';
my $prefix = '+49';

# Makes a Datebase Handler
my $dbh = DBI->connect("DBI:CSV:f_dir=$db") or die DBI::errstr;
$dbh->{csv_tables}->{phonebook} =
{
'file' => 'test.csv',
'col_names' => ["ID", "Nummer", "Vorname", "Nachname"],
};


my $sql = qq|
SELECT * FROM phonebook
|;

my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my @row = $sth->fetchrow_array()) {
print join "\t", @row;
print "\n";
}

$sth->finish();
$dbh->disconnect();

exit;
\n\n

<!--EDIT|Ronnie|1079890178-->
format_c
 2004-03-21 19:52
#31862 #31862
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Danke. Nach deiner Anregung mit den col_names hab ich nochmal genau die Doku gelesen jetzt gehts.
Code: (dl )
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
#!/usr/bin/perl
use strict;
use warnings;
use DBI;

# Declare variables
my $db = '/home/format_c/tmp/csvdb';
my $prefix = '+49';

# Makes a Datebase Handler
my $dbh = DBI->connect("DBI:CSV:f_dir=$db") or die DBI::errstr;
$dbh->{csv_tables}->{phonebook} = { file => 'Phonebook_SM.csv',
#col_names => ["id", "nummer", "name"],
eol => "\n",
sep_char => ",",
quote_char => '"',
escape_cahar => '\\',
};

my $sql = qq|
SELECT * FROM phonebook
|;

my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my @row = $sth->fetchrow_array()) {
printf "%s : %s : %s \n", @row;
}

$sth->finish();
$dbh->disconnect();

exit;

Musste nur das Format der CSV-Datei genau definieren.

Ach ja zu deiner Verwunderung vorhin hab ich auch was gefunden:

perldoc DBD::CSV
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
               skip_first_row
By default DBD::CSV assumes that col-
umn names are stored in the first row
of the CSV file. If this is not the
case, you can supply an array ref of
table names with the col_names
attribute. In that case the attribute
skip_first_row will be set to FALSE.

If you supply an empty array ref, the
driver will read the first row for
you, count the number of columns and
create column names like "col0",
"col1", ...


Gruß Alex\n\n

<!--EDIT|format_c|1079891801-->
<< >> 8 Einträge, 1 Seite



View all threads created 2004-03-20 20:14.