简体   繁体   English

SqLite 数据库在 unicode 和 PRAGMA journal_mode 设置时不会关闭

[英]SqLite database won't close when in unicode and PRAGMA journal_mode is set

My windows application (written in C) uses SQLite (using the embedded amalgamated source) for storing offline data, and have had no problems with it in a multibyte character set build.我的 windows 应用程序(用 C 编写)使用 SQLite(使用嵌入式合并源)存储离线数据,并且在多字节字符集构建中没有遇到任何问题。 I'm now converting the codebase to Unicode. SQLite is quite happily running with the Unicode build, creating SQLite databases in UTF-16LE encoding.我现在将代码库转换为 Unicode。SQLite 非常愉快地运行 Unicode 构建,以 UTF-16LE 编码创建 SQLite 数据库。

I've come across one very odd thing, though.不过,我遇到过一件非常奇怪的事情。 If I run the query PRAGMA journal_mode = DELETE (actually, setting it to ANY of the allowed values, including OFF , does the same thing), everything runs fine until the code closes the database, at which point the call to sqlite3_close() returns SQLITE_BUSY , with the error "unable to close due to unfinalised statements" .如果我运行查询PRAGMA journal_mode = DELETE (实际上,将其设置为任何允许的值,包括OFF ,做同样的事情),一切运行正常,直到代码关闭数据库,此时调用sqlite3_close()返回SQLITE_BUSY ,错误"unable to close due to unfinalised statements"

This happens even if the only query I run on the database is that single pragma command.即使我在数据库上运行的唯一查询是单个 pragma 命令,也会发生这种情况。 I can run other pragmas fine (eg "PRAGMA synchronous = NORMAL" ) and all my SQL queries, and the database will close quite happily;我可以很好地运行其他 pragma(例如"PRAGMA synchronous = NORMAL" )和我所有的 SQL 查询,并且数据库将非常愉快地关闭; it's just this journal_mode pragma that's causing this odd behaviour, and ONLY in the Unicode build of the code - the multibyte build doesn't show this problem.正是这个journal_mode pragma 导致了这种奇怪的行为,并且仅在代码的 Unicode 版本中 - 多字节版本没有显示此问题。 This happens whether the database exists already [in UTF-16LE mode], or whether it creates it on first opening.无论数据库已经存在 [以 UTF-16LE 模式],还是在第一次打开时创建它,都会发生这种情况。

Any ideas what could be wrong?任何想法可能是错误的? I'm using SQLite build v3.7.11.我正在使用 SQLite build v3.7.11。

I found the problem in the end.最后我发现了问题。 Was in my own wrapper code.在我自己的包装代码中。 As sqlite has no unicode equivalent call for sqlite3_exec() , I had let the unicode version drop through to my code that does prepare/step/finalize (for when the caller wants data back from an SQL query), but for some reason setting the journal_mode pragma causes SqLite to return some data, which it doesn't for the other pragmas I set - thus there was an unfinalised stmt for just this pragma.由于 sqlite 没有 unicode 对sqlite3_exec()的等效调用,我让 unicode 版本下降到我的代码,它确实准备/步骤/完成(当调用者想要从 SQL 查询返回数据时),但出于某种原因设置journal_mode pragma 会导致 SqLite 返回一些数据,而我设置的其他 pragma 则不会返回一些数据 - 因此只有这个 pragma 有一个未完成的 stmt。

For future reference, if you get SQLITE_BUSY when closing, you can call sqlite3_next_stmt() to get the [next] unfinalised statement, then sqlite3_sql() to get it to tell you the prepared SQL statement associated with it.为了将来参考,如果您在关闭时得到 SQLITE_BUSY,您可以调用sqlite3_next_stmt()来获取 [下一个] 未完成的语句,然后调用 sqlite3_sql( sqlite3_sql()来告诉您准备好的 SQL 语句与之关联。 Very useful!很有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM