pysome has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I got a "db commit failed" error message ,it sounds "DBD::SQLite::db commit failed: cannot commit transaction - SQL statements in progress(1) at dbdimp.c line 218 at ./test.pl line xxx.";

My Code is following:

$dbh = DBI->connect("dbi:sqlite:server=xxx;database=test","","") or di +e "$DBI::errstr\n"; $sth = $dbh->prepare(qq{select id,town from town}); my($id,$name,$n); $sth->execute() or die "error $DBI::errstr\n"; $sth->bind_columns(undef, \$id, \$name); while ($sth->fetch) { $dbh->do("insert into record(id,name) values ($id,'$name')") or die +"can't insert into record\n"; $sth->commit if $n++ % 1000; ## the xxx error point to here,but i r +eally don't know if it is a error } $sth->commit if $n $sth->finish; $dbh->disconnect;
where is it wrong? Thanks

Replies are listed 'Best First'.
Re: Cant' commit transaction???
by jZed (Prior) on Aug 17, 2007 at 05:52 UTC
    You have only one statement handle named $sth, it is the statement for your SELECT statement, not for your INSERT statement. You are trying to commit the SELECT which makes no sense. And you are trying to commit it during the fetch so it is still active. You need to prepare a second statement handle for the INSERT and commit that.
Re: Cant' commit transaction???
by roboticus (Chancellor) on Aug 17, 2007 at 11:30 UTC
    pvsome:

    Also, you'll want to read up on the prepare and execute methods in DBI, as well as the section on placeholders. Combine those three together, and I think you'll get the results you're looking for.

    ...roboticus