Android SQLite - changing journal_mode

时光总嘲笑我的痴心妄想 提交于 2019-12-21 18:13:00

问题


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


回答1:


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.

Update: As Ranjit said in the comments, this should work:

Cursor c1 = db.rawQuery("PRAGMA journal_mode=OFF", null); 
c1.close();



回答2:


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)



来源:https://stackoverflow.com/questions/5163320/android-sqlite-changing-journal-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!