Android onUpgrade database causing IllegalStateException

in dbHelper :

 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 

I save one table in an external database.

Therefore I

  • attach and copy to external database
  • delete the db file in this function and copy it from the external
  • recovery from an external database in db.

this works until the function returns to getWritableDatabase ()

Here I get this exception:

 java.lang.IllegalStateException: no transaction pending 

What's wrong? thanks tat

+1
source share
2 answers

The problem is that the onUpgrade method (as well as onCreate ) is encountered in the transaction and at the end establishes a successful transaction and completes it.

However, you have already completed the transaction in onUpgrade , which led to this error.

Relevant code from SQLiteOpenHelper :

  ... if (version != mNewVersion) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { onUpgrade(db, version, mNewVersion); } db.setVersion(mNewVersion); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } ... 
+3
source

I assume that you probably called SQLiteDatabase.endTransaction() in onUpgrade without first calling SQLiteDatabase.beginTransaction() . Although it's a little hard to say without seeing any code;)

+2
source

All Articles