微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

拥有SQLite数据库和onUpgrade

如何解决拥有SQLite数据库和onUpgrade

| 我已使用本教程来为Android创建并填充我自己的sqlite数据库。 http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications 但是,使用这些确切的方法意味着数据库不会使用以下代码升级
private static final int DB_Version = 2;

    public DbConnector(Context context){
        super(context,DB_Name,null,DB_Version);
        this.context = context;
    }
实际上,永不调用onUpgrade()方法: 感谢您的帮助     

解决方法

        这是对Joe Masilotti的感谢 http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/
private static final int DATABASE_VERSION = 1;
将构造函数更改为:
public DataBaseHelper(Context context) {
    super(context,DB_NAME,null,DATABASE_VERSION);
    this.myContext = context;
}
将ѭ3更改为(感谢@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”);
        }
    }
}
将ѭ5更改为:
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);
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。