如何使用SQLiteAssetHelper将新列或新表添加到预先填充(在资产中)的数据库中。
我最初预加载了一个包含3列的简单表:
ID
题
回答
安装应用程序后,我需要添加一些列,这些列将填充用户数据,例如标记为“收藏夹”,之前见过,已编辑...
另外,我需要添加一个新表来记录学习时间以及上次看到的问题。
这是我的代码:
import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase.db.sqlite";
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
和
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;
最佳答案
嗨,您可以使用onUpgrade
public class OpenHelper extends SQLiteOpenHelper {
private final static int DB_VERSION = 2;
public TracksDB(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//....
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
"create table session"
" (_id integer primary key autoincrement, " +
"date text not null, " +
"question text not null);";
db.execSQL(CREATE_TBL );
}
}
}
您可以检查数据库版本。如果应用程序旧版本中的数据库版本运行,请创建或更改查询查询。
SQLiteDatabase db = new Helper(context).getWritableDatabase();
if(db.getVersion()<2){
"create table session"
" (_id integer primary key autoincrement, " +
"date text not null, " +
"question text not null);";
db.execSQL(CREATE_TBL );
}
You must upgrade database version in asset folder.