简体   繁体   English

更新sqlite数据库版本?

[英]Updating sqlite database versions?

I have a database that I want to add a column to, I changed the static database version but when I run the program on my device the new column is still not there so I am guessing my onUpdate is not getting called and I am not sure why. 我有一个数据库,我想添加一个列,我更改了静态数据库版本,但是当我在我的设备上运行程序时,新列仍然没有,所以我猜我的onUpdate没有被调用,我不确定为什么。

this is how I create the database 这就是我创建数据库的方式

     private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            createTables(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w("CalendarDB", "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS Events_Table");
            onCreate(db);
        }

        private void createTables(SQLiteDatabase db){
            db.execSQL("CREATE TABLE " + EVENTS_TABLE + "(" + ID + " integer primary key autoincrement, " +
                    EVENT + " TEXT, " + LOCATION + " TEXT, " + DESCRIPTION + " TEXT, " + DATE + " TEXT, " + START + " LONG, " + END + " TEXT, " + REAL_START_TIME + " TEXT," 
                    + REAL_END_TIME + " TEXT," + COLOR + " TEXT, " + BLINK + " TEXT, " + VIBRATE + " TEXT, " + RING_TONE_PATH + " TEXT, " + ICON + " TEXT, " + REMINDERS +
                    " TEXT, " + START_WITH_REMINDER + " TEXT, " + CALENDAR_ID + " TEXT, " + EVENT_ID + " TEXT);");
        }
    }

is there something else I have to do to get the onUpdate to get called? 还有什么我需要做的才能让onUpdate被调用吗? I tried following this answer but still no result 我尝试了这个答案,但仍然没有结果

In order to upgrade the Database in Android you should increment the DATABASE_VERSION by one, in order for the SQLOpenHelper to know that it must called the onUpgrade method. 为了在Android中升级数据库,您应该将DATABASE_VERSION增加1,以便SQLOpenHelper知道它必须调用onUpgrade方法。

Rembember Rembember

This only work when you call getWritableDatabase() otherwise it won't upgrade. 这仅在您调用getWritableDatabase()时有效,否则将无法升级。 And if you change the version and call getReadableDatabase it will show an exception 如果您更改版本并调用getReadableDatabase,它将显示异常

throw new SQLiteException("Can't upgrade read-only database from version " +
                        db.getVersion() + " to " + mNewVersion + ": " + path);

But Why? 但为什么? You see when you call getWritableDatabase the code check wether the version is the same: 你看到当你调用getWritableDatabase代码时检查版本是否相同:

if (version != mNewVersion) {
                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db);
                    } else {
                        if (version > mNewVersion) {
                            onDowngrade(db, version, mNewVersion);
                        } else {
                            onUpgrade(db, version, mNewVersion);
                        }
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
}
}

Update 更新

Well turns out that it should work with getReadableDatabase() since in the code you always are getting a WrittableDatabase too. 事实证明它应该与getReadableDatabase()一起使用,因为在代码中你总是得到一个WrittableDatabase。 So it should work with both method, here is the documentation for getReadableDatabase(): 所以它应该适用于这两种方法,这里是getReadableDatabase()的文档:

Create and/or open a database. 创建和/或打开数据库。 This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. 这将是getWritableDatabase()返回的同一对象,除非某些问题(例如完整磁盘)要求以只读方式打开数据库。 In that case, a read-only database object will be returned. 在这种情况下,将返回只读数据库对象。 If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future. 如果问题得到解决,将来可能会调用getWritableDatabase(),在这种情况下,将关闭只读数据库对象,并在将来返回读/写对象。

我建议你将DATABASE_VERSION增加1,这将导致应用程序调用onUpgrade()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM