Android SQLite - changing journal_mode

2019-04-10 06:12发布

I am trying to alter the journal_mode for my database, through code. 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. 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.

Can someone suggest any alternatives to make this work through code?

Thanks, Ranjit

2条回答
我想做一个坏孩纸
2楼-- · 2019-04-10 06:43

Perhaps you are suffering from this problem that the execSQL docs talk about:

When using enableWriteAheadLogging(), journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode'" statement if your app is using enableWriteAheadLogging()

Check that you are not using write ahead logging and then it might work.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-10 06:46

It can be done with the next command:

db.rawExecSQL("PRAGMA journal_mode=OFF;");

(No need to create a Cursor as it is suggested in one of the comments to Robert's answer)

查看更多
登录 后发表回答