自己的SQLite数据库和onUpgrade [英] Own SQLite Database and onUpgrade

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

问题描述

我用这个教程,以创建和填充自己的SQLite数据库为Android。 的http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications

I've used this tutorial in order to create and populate my own SQLite database for android. http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications

不过,使用这些精确的方法意味着数据库犯规具有以下code升级

However, using these exact methods mean that the database doesnt upgrade with the following code

private static final int DB_Version = 2;

    public DbConnector(Context context){
        super(context, DB_Name, null, DB_Version);
        this.context = context;
    }

其实方法onUpgrade()不会被调用:(

In fact the method onUpgrade() is never called :(

感谢您的帮助

推荐答案

下面是答案感谢乔Masilotti上 <一href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/" rel="nofollow">http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/

Here is the answer thanks to Joe Masilotti on http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/

private static final int DATABASE_VERSION = 1;

构造函数更改为:

Change the constructor to:

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

更改的CreateDatabase 来(感谢@kondortek):

Change the createDataBase to (thanks @kondortek):

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        Log.v("DB Exists", "db exists");
        // By calling this method here onUpgrade will be called on a
        // writeable database, but only if the version number has been
        // bumped
        this.getWritableDatabase();
    }
    dbExist = checkDataBase();
    if (!dbExist) {
        // By calling this method and empty database will be created into
        // the default system path of your application so we are gonna be
        // able to overwrite that database with our database.
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

修改 onUpgrade 来:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        Log.v("Database Upgrade", "Database version higher than old.");
    myContext.deleteDatabase(DB_NAME);
}

这篇关于自己的SQLite数据库和onUpgrade的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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