简体   繁体   中英

How to add a new column or table to sqlite database pre-populated in the assets and using SQLiteAssetHelper

How can I add a new column or new table to the database that pre-populated (in the assets) using SQLiteAssetHelper. I originally preload a simple table with 3 columns:

  • id
  • question
  • answer

Once installed the app, I need to add some columns that will be populated with user data, like marked as favorite, seen before, edited... Also I need to add a new table that record the time of the study session, and last question seen. here's my code:

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);
 }
}

and

public class DatabaseAccess {
private SQLiteOpenHelper openHelper;

private SQLiteDatabase database;
private static DatabaseAccess instance;

Hi you can use 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 );
        }
    }
}

You can check database Version. If database version in app old version run create or alter query query.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM