Android的onUpgrade数据库导致IllegalStateException异常 [英] Android onUpgrade Database causing IllegalStateException

查看:140
本文介绍了Android的onUpgrade数据库导致IllegalStateException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 dbHelper

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 

功能我一个表保存到外部数据库。

function I save one table to an external database.

所以我


  1. 连接并复制到外部数据库

  2. 删除此功能的数据库文件,并从外部
  3. 复制
  4. 从外部数据库恢复到数据库。

这工作,直到函数返回到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆