我正在使用我们的Android应用程序发送数据库文件。为此,我按How to ship an Android application with a database?回答Danny Remington - OMS。
现在我必须提供新版本因为数据库模式已经更改,所以首先我将新的db文件从assets复制到data / data目录,并在onUpgrade
方法中编写如下的insert语句SqliteOpenHelper
班级:
//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();
但是insert语句抛出异常android.database.sqlite.SQLiteException: no such table: DB_new.table1
我如何才能实现这一目标。请帮助我。
答案 0 :(得分:1)
在你的sqlite帮助器中,有一个方法onUpgrade
,当你传递给超级构造函数(SqliteOpenHelper
)的版本发生变化时会触发该方法。因此,您需要更改此数字,以便onUpgrade
触发,然后将您的代码放入"重新安装"这个方法里面的数据库。