更新SQLite数据库的版本? [英] Updating sqlite database versions?

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

问题描述

我有,我想添加一个列到数据库中,我改变了静态的数据库版本,但是当我在我的设备上运行该程序的新列仍然是不存在,所以我猜我的的onupdate 是没有得到所谓的,我不知道为什么。

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.

这是我如何创建数据库

     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);");
        }
    }

有没有别的东西,我必须做的就是的onupdate 来被调用?我尝试如下的<一个href="http://stackoverflow.com/questions/8133597/android-upgrading-db-version-and-adding-new-table">this回答但还是没有结果。

is there something else I have to do to get the onUpdate to get called? I tried following this answer but still no result

推荐答案

为了提升数据库Android中你应该增加的database_version将由之一,为了使SQLOpenHelper知道它必须调用onUpgrade方法。

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.

Rembember

当你调用getWritableDatabase(这唯一的工作),否则将无法升级。如果你更改版本,并呼吁getReadableDatabase它会显示一个异常

This only work when you call getWritableDatabase() otherwise it won't upgrade. And if you change the version and call getReadableDatabase it will show an exception

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

但是,为什么? 你看,当你调用<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper">getWritableDatabase在code检阉的版本是一样的:

But Why? You see when you call getWritableDatabase the code check wether the version is the same:

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();
}
}

更新

那么事实证明,它应与g​​etReadableDatabase()在$ C $工作,因为C你总是得到一个WrittableDatabase了。因此,它应与这两个方法,这里是getReadableDatabase()的文档:

Well turns out that it should work with getReadableDatabase() since in the code you always are getting a WrittableDatabase too. So it should work with both method, here is the documentation for getReadableDatabase():

创建和/或打开数据库。这将是相同的对象返回的   通过getWritableDatabase(),除非一些问题,如磁盘已满,   需要打开数据库只读。在这种情况下,一个   只读数据库对象将被退回。如果问题是固定的,一   到getWritableDatabase()调用的未来可能会成功,在这种情况下,   只读数据库对象将被关闭,在读/写对象   今后将被退回。

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. 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.

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

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