版权声明:欢迎传播,请标明出处。
https://cloud.tencent.com/developer/article/1402721
sqlite关闭时出错.
代码语言:javascript复制 int res = sqlite3_close(_sqliteDB);
if(res)
{
cout << "can't close database: " << sqlite3_errmsg(_sqliteDB) << endl;
return -1;
}
其中 res = 5, errmsg是"unable to close due to unfinalized statements or unfinished backups".
查了下原因, 是因为
Applications must finalize all prepared statements and close all BLOB handles associated with the sqlite3 object prior to attempting to close the object. If sqlite3_close() is called on a database connection that still has outstanding prepared statements or BLOB handles, then it returns SQLITE_BUSY.
其实是代码里打开一个sqlite3_stmt/sqlite3_blob, 就要及时关闭, 否则就会报这个错误, 就像对象只创建, 没有析构会造成内存泄露一样.
sqlite3_prepare_v2 要对应一个sqlite3_finalize,
sqlite3_blob_open要对应一个sqlite3_blob_close.
Applications should finalize all prepared statements, close all BLOB handles, and finish all sqlite3_backup objects associated with the sqlite3 object prior to attempting to close the object. If sqlite3_close_v2() is called on a database connection that still has outstanding prepared statements, BLOB handles, and/or sqlite3_backup objects then it returns SQLITE_OK and the deallocation of resources is deferred until all prepared statements, BLOB handles, and sqlite3_backup objects are also destroyed.
https://stackoverflow.com/questions/2144757/sqlite3-close-returns-error-code-5
http://www.sqlite.org/c3ref/close.html