[英]Android SQLite - changing journal_mode
I am trying to alter the journal_mode for my database, through code.我正在尝试通过代码更改我的数据库的 journal_mode。 I tried SQLiteDatabase.execSQL("PRAGMA journal_mode=OFF") but it fails because, the expression "PRAGMA journal_mode=OFF" returns a result (I verified this from the terminal), and so Android thinks this is a query and complains that I should be using execQuery instead.
我试过 SQLiteDatabase.execSQL("PRAGMA journal_mode=OFF") 但它失败了,因为表达式 "PRAGMA journal_mode=OFF" 返回一个结果(我从终端验证了这一点),所以 Android 认为这是一个查询并抱怨我应该使用 execQuery 代替。 But what I am trying to execute isn't a query.
但我试图执行的不是查询。
I also tried compiling the pragma expression to a SQLiteStatement and invoked the execute method, but same result.我还尝试将 pragma 表达式编译为 SQLiteStatement 并调用 execute 方法,但结果相同。
Can someone suggest any alternatives to make this work through code?有人可以建议任何替代方案来通过代码完成这项工作吗?
Thanks, Ranjit谢谢,兰吉特
Perhaps you are suffering from this problem that the execSQL docs talk about:也许您正在遭受execSQL 文档谈论的这个问题:
When using enableWriteAheadLogging(), journal_mode is automatically managed by this class.
使用 enableWriteAheadLogging() 时,journal_mode 由该类自动管理。 So, do not set journal_mode using "PRAGMA journal_mode'" statement if your app is using enableWriteAheadLogging()
因此,如果您的应用程序使用 enableWriteAheadLogging(),请不要使用“PRAGMA journal_mode'”语句设置 journal_mode
Check that you are not using write ahead logging and then it might work.检查您没有使用预写日志记录,然后它可能会起作用。
Update: As Ranjit said in the comments, this should work:更新:正如 Ranjit 在评论中所说,这应该有效:
Cursor c1 = db.rawQuery("PRAGMA journal_mode=OFF", null);
c1.close();
It can be done with the next command:可以使用下一个命令完成:
db.execSQL("PRAGMA journal_mode=OFF;");
(No need to create a Cursor
as it is suggested in one of the comments to Robert's answer ) (无需创建
Cursor
因为它在罗伯特回答的评论之一中建议)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.