问题
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