SQLite数据库关闭后准备但stmt仍然有效?(SQLite DB closed after prepare but stmt still works?)

系统教程 行业动态 更新时间:2024-06-14 16:58:29
SQLite数据库关闭后准备但stmt仍然有效?(SQLite DB closed after prepare but stmt still works?)

另外,DB和注册函数的sqlite_master表仍然可用。 这只是stmt访问内存的一种情况,它尚未被覆盖,或者准备将详细信息写入stmt中,这意味着它不需要sqlite3 *结构。

#include "sqlite3.h" //--------------------------------------------------------------------------- void Odd(sqlite3_context *ctx,int nargs,sqlite3_value **values) { sqlite3_result_int(ctx,sqlite3_value_int(values[0])%2); } //--------------------------------------------------------------------------- int _tmain(int argc,_TCHAR* argv[]) { sqlite3 *DB; if (sqlite3_open_v2("c:/SQLiteData/MyDB.db",&DB,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) return 1; sqlite3_create_function_v2(DB,"Odd",-1,SQLITE_UTF16 | SQLITE_DETERMINISTIC,NULL, &Odd,NULL,NULL,NULL); sqlite3_stmt *stmt; if (sqlite3_prepare16_v2(DB,L"select * from sqlite_master where Odd(rowid)", -1,&stmt,NULL)!=SQLITE_OK) return 2; if (sqlite3_close_v2(DB)!=SQLITE_OK) return 3; int Count=0; while (sqlite3_step(stmt)==SQLITE_ROW) Count++; return 0; }

Also, the sqlite_master table for DB and registered functions still seem available. Is this just a case of the stmt accessing memory that hasn't been overwritten yet or does the prepare write details into the stmt that means it doesn't subsequently require the sqlite3* structure.

#include "sqlite3.h" //--------------------------------------------------------------------------- void Odd(sqlite3_context *ctx,int nargs,sqlite3_value **values) { sqlite3_result_int(ctx,sqlite3_value_int(values[0])%2); } //--------------------------------------------------------------------------- int _tmain(int argc,_TCHAR* argv[]) { sqlite3 *DB; if (sqlite3_open_v2("c:/SQLiteData/MyDB.db",&DB,SQLITE_OPEN_READWRITE,NULL)!=SQLITE_OK) return 1; sqlite3_create_function_v2(DB,"Odd",-1,SQLITE_UTF16 | SQLITE_DETERMINISTIC,NULL, &Odd,NULL,NULL,NULL); sqlite3_stmt *stmt; if (sqlite3_prepare16_v2(DB,L"select * from sqlite_master where Odd(rowid)", -1,&stmt,NULL)!=SQLITE_OK) return 2; if (sqlite3_close_v2(DB)!=SQLITE_OK) return 3; int Count=0; while (sqlite3_step(stmt)==SQLITE_ROW) Count++; return 0; }

最满意答案

该文件说:

如果数据库连接与未初始化的预处理语句相关联,则sqlite3_close()将打开数据库连接并返回SQLITE_BUSY。 如果调用sqlite3_close_v2()时使用未定义的预处理语句...,那么数据库连接将变为不可用的“僵尸”,当最后一个准备好的语句完成或最后一个sqlite3_backup完成时,它将自动解除分配。 sqlite3_close_v2()接口旨在用于垃圾收集的主机语言,以及调用析构函数的顺序是任意的。

但是你没有使用这种语言。 你不应该尝试访问僵尸; 你的申请

应该在尝试关闭对象之前完成与sqlite3对象关联的所有准备好的语句。

The documentation says:

If the database connection is associated with unfinalized prepared statements … then sqlite3_close() will leave the database connection open and return SQLITE_BUSY. If sqlite3_close_v2() is called with unfinalized prepared statements …, then the database connection becomes an unusable "zombie" which will automatically be deallocated when the last prepared statement is finalized or the last sqlite3_backup is finished. The sqlite3_close_v2() interface is intended for use with host languages that are garbage collected, and where the order in which destructors are called is arbitrary.

But you are not using such a language. You should not try to access the zombie; your application

should finalize all prepared statements … associated with the sqlite3 object prior to attempting to close the object.

更多推荐

本文发布于:2023-04-15 03:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/183044922aacbf7fb4d474430e5ffa7e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   stmt   SQLite   DB   works

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!