On 6 Sep 2001, at 23:45, Guido Ostkamp wrote:
>
> 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?
I could reproduce your problem with Perl 5.6, DBI 1.14, and DBD::CSV 0.1023
on win2k. When I am using your code with DBD::ODBC and Access 2000, this
problem does not occur. So, I think that you are doing *nothing*
wrong, but rather that $y within the C-style loop conditional is going
out of scope for one of the objects behind DBI after it has been incremented.
Perhaps this is a bug in Perl, or in the implementations of the
modules in question. The only workaround seems to be avoiding C-style
loops.
Bodo
>
> 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++;
> }