android – 更新sqlite数据库版本?

android – 更新sqlite数据库版本?,第1张

概述我有一个数据库,我想添加一个列,我更改了静态数据库版本但是当我在我的设备上运行程序时,新列仍然没有,所以我猜我的onUpdate没有被调用,我不确定为什么. 这就是我创建数据库的方式 private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) 我有一个数据库,我想添加一个列,我更改了静态数据库版本但是当我在我的设备上运行程序时,新列仍然没有,所以我猜我的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","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被调用吗?我试过跟随this answer,但仍然没有结果

解决方法 为了在AndroID中升级数据库,您应该将DATABASE_VERSION增加1,以便sqlOpenHelper知道它必须调用onUpgrade方法.

Rembember

这仅在您调用getWritableDatabase()时有效,否则将无法升级.如果更改版本并调用getReadableDatabase,它将显示异常

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

但为什么?
你看到当你拨打getWritableDatabase代码检查时,版本是相同的:

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

更新

事实证明它应该与getReadableDatabase()一起使用,因为在代码中你总是得到一个WrittableDatabase.所以它应该适用于这两种方法,这里是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. 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.

总结

以上是内存溢出为你收集整理的android – 更新sqlite数据库版本?全部内容,希望文章能够帮你解决android – 更新sqlite数据库版本?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存