简体   繁体   English

如何将新列或表添加到资源中预先填充的sqlite数据库并使用SQLiteAssetHelper

[英]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. 如何使用SQLiteAssetHelper将新列或新表添加到预填充(在资产中)的数据库中。 I originally preload a simple table with 3 columns: 我最初预装了一个包含3列的简单表:

  • id 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 嗨,你可以使用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. 如果app旧版本中的数据库版本运行创建或更改查询查询。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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