gpt4 book ai didi

perl - DBD :SQLite disconnect

转载 作者:行者123 更新时间:2023-12-05 00:19:33 24 4
gpt4 key购买 nike

我写了下面的代码:

$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();

并得到这个警告:
closing dbh with active statement handles at mysqlib.pl line 23

有人可以解释此警告消息的含义吗?

最佳答案

您发布的代码是正确的(第 1 行的语法错误除外)

此外,您的错误位于“23”行,并且您发布的代码没有 23 行。

我认为错误在您的代码中的其他地方。

编辑:
您使用什么版本的 SQLite 模块?我凝视了一下,发现: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".



问候,

杰弗里德

关于perl - DBD :SQLite disconnect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com