Hello,

I am trying to get DBI and DBD:CSV to work on my system (perl 5.6, DBI
1.20 and DBD::CSV 0.1027 on a SuSE Linux 7.2).

I have setup a small CSV table with three rows and I am trying to fetch
the values from the DB in a loop using a SELECT with condition.

Howver, this does not work, if I use the loop variable as argument for the
$sth->execute (I only get one row back instead of three). Strangely,
whenever I use a different variable, it works, but I cannot find the
reason why (please see code below).

Does somebody has an idea of what I am doing wrong?

Thanks in advance,

Guido

+++ Here is, what I get +++

Part 1 does not work ...
y = 1
Found result row: id = 1, name = One
y = 2
y = 3

Part 2 works ...
y = 1
Found result row: id = 1, name = One
y = 2
Found result row: id = 2, name = Two
y = 3
Found result row: id = 3, name = Three

+++ snip +++ snap +++

#!/usr/bin/perl -w

use DBI;
# DBI->trace(1);

unlink ("/tmp/xxx");
my $dbh = DBI->connect("DBI:CSV:f_dir=/tmp");
my $table = "xxx";

$dbh->do("CREATE TABLE $table (id INTEGER, name CHAR(64))");
$dbh->do("INSERT INTO $table VALUES (1, 'One')");
$dbh->do("INSERT INTO $table VALUES (2, 'Two')");
$dbh->do("INSERT INTO $table VALUES (3, 'Three')");

my ($query) = "SELECT * FROM $table WHERE id = ?";
my ($sth) = $dbh->prepare($query);
my ($id, $name);

print "\nPart 1 does not work ...\n";

for ($y = 1; $y <= 3; $y++) {
        print "y = $y\n";
        $sth->execute($y);
        $sth->bind_columns(undef, \$id, \$name);
        if ($sth->fetch) {
                print("Found result row: id = $id, name = $name\n");
        }
        $sth->finish();
}

print "\nPart 2 works ...\n";
$y = 1;
for ($x = 1; $x <= 3; $x++) {
        print "y = $y\n";
        $sth->execute($y);
        $sth->bind_columns(undef, \$id, \$name);
        if ($sth->fetch) {
                print("Found result row: id = $id, name = $name\n");
        }
        $sth->finish();
        $y++;
}


Reply via email to