onUpgrade数据库Android版本号不增加 [英] onUpgrade database Android version number not increasing

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

问题描述

我想在我的Andr​​oid应用程序来更新我的数据库。当我更新的版本号,onUpgrade被调用,但版本号不会增加,所以我每次访问数据库时,onUpgrade被调用。这里是我的code:

I am trying to update my database in my Android application. When I update the version number, onUpgrade gets called, but the version number doesn't increase, so every time I access the database, onUpgrade gets called. Here is my code:

private final static int DB_VERSION = 8;

        public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
    }   
        @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "in onCreate");
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
        myContext.deleteDatabase(DB_NAME);
        Log.d(TAG, "the version is " + db.getVersion());
        db.setVersion(newVersion);
        Log.d(TAG, "the version is " + db.getVersion());
        onCreate(db);           
    }

任何人都知道为什么会这样?

Anyone know why this is happening?

推荐答案

我最终加入另一个数据库添加到资产的文件夹。我知道它不是最好的解决方案,但它的工作原理。当应用程序升级这个时候,它只是写入新的数据库 - 它有一个新的名字 - 而不是旧的。我检查,如果旧的存在,如果是的话,我将其删除。

I ended up adding another database to the assets folder. I know its not the best solution, but it works. When the app is upgraded this time, it just writes the new database - it has a new name - instead of the old one. I check if the old one exists and if it does, I delete it.

这篇关于onUpgrade数据库Android版本号不增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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