Android cannot update my database when the first query uses readonly database

So, I've been relying on the automated (almost) onCreate / onUpgrade my SQLite database for so long. Everything worked fine, and I just had to change the version number of the database so that it automatically started onUpgrade .

But now I came across this exception when trying to add a column to my database and changing the version number:

 SQLiteException: Can't upgrade read-only database from version X to Y 

Looking at my code, I am sure that the first query of my application is a SELECT query, and I use getReadableDatabase to open a connection for it because the query does not require write access.

So, I was wondering, how can I solve such a problem? Should I use getWritableDatabase all the time? (as a workaround that works to pass my update). Of course, if both methods exist because they are designed to be used and the connection is optimized for read-only access when necessary? I would not want to lose profit.

+1
source share
3 answers

The workaround I used: has a "start" method of my application that only does getWritableDatabase() and closes it so that it activates all the necessary create / update, if necessary.

0
source

In my case, there was a syntax error: one of the fields was a SQL reserved word. But in order to see this syntax error message, I had to add

 dbHelper.getWritableDatabase().close(); 

to MainActivity.onCreate() .

0
source

Some time column name is repeated during instruction creation.

 create table tbl (Col1 TEXT,Col2 INTEGER,Col3 INTEGER,Col1 INTEGER,Col5 INTEGER); 

Like here, Col1 repeats twice ...

-one
source

All Articles