android中的SQLite在创建数据库、创建表后,如何给表增加列数,或者更新版本?
如题,代码如下:privateSQLiteDatabasedb;db=(newDatabaseHelper(S2iCamera.this,"mytable",null,1...
如题,代码如下:
private SQLiteDatabase db;
db = (new DatabaseHelper(S2iCamera.this,"mytable",null,1)).getWritableDatabase();
db.execSQL("CREATE TABLE if not exists saveS2i (_id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, date TEXT, dateFormat TEXT);");
db.close();
在创建了表mytable以后,我需要修改程序,更新数据库版本,以新增列数,比方说这样:
private SQLiteDatabase db;
db = (new DatabaseHelper(S2iCamera.this,"mytable",null,1)).getWritableDatabase();
db.execSQL("CREATE TABLE if not exists saveS2i (_id INTEGER PRIMARY KEY AUTOINCREMENT, new TEXT, path TEXT, date TEXT, dateFormat TEXT);");
db.close();
新增加了new列,但是更新后数据库里的mytable表并没有增加new列,请问如何能给已经发布的程序增加列数? 展开
private SQLiteDatabase db;
db = (new DatabaseHelper(S2iCamera.this,"mytable",null,1)).getWritableDatabase();
db.execSQL("CREATE TABLE if not exists saveS2i (_id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, date TEXT, dateFormat TEXT);");
db.close();
在创建了表mytable以后,我需要修改程序,更新数据库版本,以新增列数,比方说这样:
private SQLiteDatabase db;
db = (new DatabaseHelper(S2iCamera.this,"mytable",null,1)).getWritableDatabase();
db.execSQL("CREATE TABLE if not exists saveS2i (_id INTEGER PRIMARY KEY AUTOINCREMENT, new TEXT, path TEXT, date TEXT, dateFormat TEXT);");
db.close();
新增加了new列,但是更新后数据库里的mytable表并没有增加new列,请问如何能给已经发布的程序增加列数? 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
//定义升级函数
private void upgradeDatabaseToVersion1(SQLiteDatabase db) {
// Add 'new' column to mytable table.
db.execSQL("ALTER TABLE mytable ADD COLUMN new TEXT");
}
//重写onUpgrade
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to " + currentVersion + ".");
switch (oldVersion) {
case 0:
if (currentVersion <= 1) {
return;
}
db.beginTransaction();
try {
upgradeDatabaseToVersion1(db);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(TAG, ex.getMessage(), ex);
break;
} finally {
db.endTransaction();
}
return;
}
Log.e(TAG, "Destroying all old data.");
dropAll(db);
onCreate(db);
}
-----------------------------------------------------------------------------------------------------
安卓精英团为你解答
安卓精英团欢迎各位精英加入
private void upgradeDatabaseToVersion1(SQLiteDatabase db) {
// Add 'new' column to mytable table.
db.execSQL("ALTER TABLE mytable ADD COLUMN new TEXT");
}
//重写onUpgrade
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to " + currentVersion + ".");
switch (oldVersion) {
case 0:
if (currentVersion <= 1) {
return;
}
db.beginTransaction();
try {
upgradeDatabaseToVersion1(db);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(TAG, ex.getMessage(), ex);
break;
} finally {
db.endTransaction();
}
return;
}
Log.e(TAG, "Destroying all old data.");
dropAll(db);
onCreate(db);
}
-----------------------------------------------------------------------------------------------------
安卓精英团为你解答
安卓精英团欢迎各位精英加入
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询