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

问题描述

我有一个数据库,我想添加一个列,我改变了静态数据库版本,但是当我在我的设备上运行该程序的新列仍然不存在,所以我猜我的 onUpdate



这是我如何创建数据库



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,升级数据库从版本+ oldVersion
+到
+ newVersion +,这将销毁所有旧数据);
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 + ,
+ 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 来调用?我尝试了,但仍然没有结果

解决方案

为了升级Android中的数据库,您应该将DATABASE_VERSION增加一,以便SQLOpenHelper知道它必须调用



这只适用于调用getWritableDatabase()方法时,否则不会升级。如果你改变版本并调用getReadableDatabase它会显示一个异常

  throw new SQLiteException数据库从版本+ 
db.getVersion()+到+ mNewVersion +:+路径);

但为什么?
您会看到当您调用代码检查版本是一样的:

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

更新 >

很明显,它应该使用getReadableDatabase(),因为在代码中,你总是得到一个WrittableDatabase。所以它应该与两个方法,这里是getReadableDatabase()的文档:


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.

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

Rembember

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

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

Update

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():

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

09-02 18:30