1

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;
AndroPro
  • 15
  • 1
  • 7

1 Answers1

0

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.
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • Thank you Ahmed, but your solution works only with "extends SQLiteOpenHelper" and it doesn't work with "extends SQLiteAssetHelper". – AndroPro Oct 19 '16 at 13:55