Android的onUpgrade数据库导致IllegalStateException异常 [英] Android onUpgrade Database causing IllegalStateException
本文介绍了Android的onUpgrade数据库导致IllegalStateException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 dbHelper
:
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
功能我一个表保存到外部数据库。
function I save one table to an external database.
所以我
- 连接并复制到外部数据库
- 删除此功能的数据库文件,并从外部 复制
- 从外部数据库恢复到数据库。
这工作,直到函数返回到getWritableDatabase()
this works until the function returns to getWritableDatabase()
在这里,我得到这个异常:
Here I get this exception:
java.lang.IllegalStateException: no transaction pending
什么是错的?谢谢
塔塔
What is wrong? Thanks Tata
推荐答案
的问题是, onUpgrade
办法(以及的onCreate
)在事务内发生,并且在端设置交易是成功的,并结束其
The issue is that the onUpgrade
method (as well as onCreate
) occurs within a transaction, and at the end sets the transaction to be successful and ends it.
但是,你已经结束了 onUpgrade
交易,造成此错误。
However, you have already ended the transaction in onUpgrade
, resulting in this error.
从相关code 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();
}
}
...
这篇关于Android的onUpgrade数据库导致IllegalStateException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文