这就是我创建数据库的方式
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数据库版本?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)