将数据传输到新的数据库版本android
我正在尝试编写一个函数,它将获取数据库中的当前信息,并使用onUpgrade方法将其放入新版本中。这就是我所拥有的将数据传输到新的数据库版本android,android,upgrade,transfer,alter,sqlite,Android,Upgrade,Transfer,Alter,Sqlite,我正在尝试编写一个函数,它将获取数据库中的当前信息,并使用onUpgrade方法将其放入新版本中。这就是我所拥有的 public void updateTable(SQLiteDatabase db) { Cursor c = db.rawQuery( "select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'", nul
public void updateTable(SQLiteDatabase db) {
Cursor c = db.rawQuery(
"select DISTINCT tbl_name from sqlite_master where tbl_name = '"
+ tableName + "'", null);
if (c != null && c.getCount() > 0) {
c = db.rawQuery("select * from " + tableName, null);
ArrayList<Transaction> tempList = new ArrayList<Transaction>();
while (c.moveToNext()) {
Transaction t = createTransaction(c);
if (t != null)
tempList.add(t);
}
dropTable(db);
createTable(db);
for (Transaction t : tempList) {
addOccurrence(t);
}
} else {
throw new RuntimeException("Couldn't find new table for updating");
}
}
问题是,我从onUpgrade方法获得的数据库中没有任何内容!所以我只是删除了我的数据库。。。。有没有办法做到这一点,或者我必须依赖ALTER语句的有限使用。谢谢
public void onUpgrade(SQLiteDatabase new_db, int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
System.out.println("onUpgrade()");
new_db.execSQL("DROP TABLE IF EXISTS " + TableName + ";");
onCreate(new_db);
}
public void onUpgrade(SQLiteDatabase new_db, int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
System.out.println("onUpgrade()");
new_db.execSQL("DROP TABLE IF EXISTS " + TableName + ";");
onCreate(new_db);
}