1
我正在运输数据库文件与我们的Android应用程序。为此我跟着How to ship an Android application with a database?回答了Danny Remington - OMS。更新SQLite数据库时,应用程序与数据库一起发布的新版本
现在我必须提供新版本,该数据库架构发生了变化,所以首先我从资产数据/数据目录复制新的数据库文件,写一个INSERT语句作为SqliteOpenHelper
类的onUpgrade
方法如下:
//rename the old database
new File(DB_PATH + DATABASE_NAME).renameTo(new File(DB_PATH + "DB_old"));
try
{
//copting new db file from assets to data/data dir
copyDataBase();
}
catch (IOException e)
{
e.printStackTrace();
}
SQLiteDatabase DB_old = SQLiteDatabase.openDatabase(DB_PATH + "DB_old", null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase DB_new = SQLiteDatabase.openDatabase(DB_PATH + "DB_new", null, SQLiteDatabase.OPEN_READWRITE);
//insert statement from old to new db
String query = "INSERT INTO DB_new.table1(user_id,user_name) SELECT * FROM DB_old.table1";
DB_new.execSQL(query);
DB_old.close();
DB_new.close();
//deleting old db file
new File(DB_PATH + "DB_old").delete();
但插入语句抛出一个异常android.database.sqlite.SQLiteException: no such table: DB_new.table1
我如何能实现this.Please帮助我在此。