简体   繁体   English

Android SQLite - 改变 journal_mode

[英]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.

相关问题 Android.database.sqllite.SQLiteDatabaseLocked异常:数据库已锁定(代码5):,正在编译:PRAGMA journal_mode - Android.database.sqllite.SQLiteDatabaseLocked Exception:database is locked (code 5): ,while compiling:PRAGMA journal_mode 无法在Android上的SQLite中更改日记模式 - Can't change journal mode in SQLite on Android sqlite 的持久日志模式从什么时候开始成为 Android 中的默认日志模式? - Since when does sqlite's persist journal mode become the default journal mode in Android? SQLiteDiskIOException:磁盘 I/O 错误(代码 1802):,编译时:PRAGMA journal_mode - SQLiteDiskIOException: disk I/O error (code 1802): , while compiling: PRAGMA journal_mode 无法打开数据库文件(代码14):,编译时:PRAGMA journal_mode - Unable to open database file (code 14): , while compiling: PRAGMA journal_mode Android SQLite Journal行为发生了变化? - Android SQLite Journal behaviour changed? Android 中的“-journal”SQLite 数据库是什么? - What is the "-journal" SQLite database in Android? 使用SQLite浏览器创建Android数据库日志文件 - Creating Android database journal file with SQLite browser 如何禁用 Android SQLite 日志文件? - How to disable Android SQLite Journal file? SQLite:具有附加数据库的预写日志记录(WAL日志模式) - SQLite: Write-Ahead Logging (WAL journal mode) with attached database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM