android数据库onupgrade,Android Studio SQLite onUpgrade

Hi I'm currently working on a language dictionary app. The database is from SQLite db which I input manually. The feature of the app is users can ask any words that is not on the list to be added, so I have to update the database manually everytime app updated.

The problem is, everytime I change the db data, the list is not updated, when I change the version number, the app crashed.

How can I handle this problem? Tutorials out there only showing how to do it on realtime input or adding columns while main only to add rows and on already existing db. Thank you.

Here is my DataBaseHelper class

public class DataBaseHelper extends SQLiteAssetHelper {

private static final String DB_Name = "kamus_db";

private static final int DB_VER = 1;

private static final String TB_DATA = "tb_data";

private static final String COL_ID = "_id";

private static final String COL_TURKI = "turki";

private static final String COL_ISTILAH ="istilah";

private static final String COL_INDONESIA = "indonesia";

private static DataBaseHelper dbInstance;

private static SQLiteDatabase db;

private DataBaseHelper(Context ctx) {

super(ctx, DB_Name, null, DB_VER);

}

static DataBaseHelper getInstance(Context ctx) {

if (dbInstance == null) {

dbInstance = new DataBaseHelper(ctx);

db = dbInstance.getWritableDatabase();

} return dbInstance;

}

@Override

public synchronized void close() {

super.close();

if (dbInstance != null) {

dbInstance.close();

}

}

List getAllKamus() {

List kamusList = new ArrayList<>();

Cursor cursor = db.query(TB_DATA, new String[] {

COL_ID, COL_TURKI, COL_ISTILAH, COL_INDONESIA

}, null, null, null, null, null);

if (cursor.getCount() >= 1 ) {

cursor.moveToFirst();

do {

Kamus kamus = new Kamus();

kamus.setTurki(cursor.getString(cursor.getColumnIndexOrThrow(COL_TURKI)));

kamus.setIstilah(cursor.getString(cursor.getColumnIndexOrThrow(COL_ISTILAH)));

kamus.setIndonesia(cursor.getString(cursor.getColumnIndexOrThrow(COL_INDONESIA)));

kamusList.add(kamus);

} while (cursor.moveToNext());

} return kamusList;

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String TAG = "TAG";

Log.w(TAG, "Upgrading database. Existing contents will be lost. ["

+ oldVersion + "] -> [" + newVersion + "]");

db.execSQL("DROP TABLE " + TB_DATA);

onCreate(db);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值