【问题标题】:How to add a new column or table to sqlite database pre-populated in the assets and using SQLiteAssetHelper如何将新列或表添加到预先填充在资产中并使用 SQLiteAssetHelper 的 sqlite 数据库
【发布时间】:2017-02-28 04:30:12
【问题描述】:

如何向使用 SQLiteAssetHelper 预填充(在资产中)的数据库添加新列或新表。 我最初预加载了一个包含 3 列的简单表格:

  • 身份证
  • 问题
  • 回答

安装应用程序后,我需要添加一些将填充用户数据的列,例如标记为收藏、以前看过、已编辑... 我还需要添加一个记录学习时间的新表,以及看到的最后一个问题。 这是我的代码:

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;

【问题讨论】:

    标签: java android mysql sqlite challenge-response


    【解决方案1】:

    您好,您可以使用 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.
    

    【讨论】:

    • 谢谢 Ahmed,但您的解决方案仅适用于“扩展 SQLiteOpenHelper”,不适用于“扩展 SQLiteAssetHelper”。
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 2016-09-12
    • 2021-06-30
    • 2021-04-04
    • 2017-03-28
    • 1970-01-01
    • 2020-07-30
    • 2012-04-02
    相关资源
    最近更新 更多