开发者

DBD:SQLite disconnect

I wrote the below code:

$dbh = DBI->connect('dbi:SQLite:mysqlite.db', "", "") || die "Cannot connect: $DBI::errstr";

my $sth = $dbh->prepare("select value1, value2 from valus_table where value2 = 4");

$sth->execute();

while (my @row = $sth->fetchrow_array) {
    print $row[0], $row[1], "\n";
}

$sth->finish;

$dbh->disconnect();

a开发者_开发问答nd get this warning:

closing dbh with active statement handles at mysqlib.pl line 23

Could someone explain the meaning of this warning message?


the code you posted is correct (except of a syntax error on line 1)

Also your error is on line "23" and your posted code doesn't have 23 lines.

I think the error is somewhere else in your code.

Edit: What version do you use of the SQLite module? I goggled a bit and found that: http://www.perlmonks.org/?node_id=665714

The problem is that DBD::SQlite->disconnect() method execute sqlite3_close() function. This function return SQLITE_BUSY in case if there are any active statement. From API: "Applications should finalize all prepared statements and close all BLOBs associated with the sqlite3 object prior to attempting to close the sqlite3 object." Currently DBD::SQLite can finalize statements only via DESTROY method. In simplest case you can always use "undef $sth" or wait untill it goes out of scope which will finalize statement. But if you prepared statement via cache (prepare_cached) it will not work for you, because statement is till inside DBI cache. In this case we can call DESTROY on our cached statement only via DESTROY for database handler. And we can achieve it by "undef $dbh". "undef $dbh" - will close all cached statements and close database without any errors. Conclusion: avoid using $dbh->disconnect() for DBD::SQLite, instead use "undef $dbh".

Regards,

jfried

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜