处理预填充数据库上的 sqlite 数据库升级 [英] Handle sqlite database upgrade on pre-populated database

查看:41
本文介绍了处理预填充数据库上的 sqlite 数据库升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 android studio 中发布了我的 android 应用程序的新版本后遇到了问题.

I'm having issues after releasing a new version of my android app in android studio.

问题来了:我有一个带有 SQlite 数据库的测验应用程序,其中问题和答案已填充 SQLite 数据库浏览器.现在,当我向数据库表添加新问题并创建新的数据库版本时,数据库似乎没有反映在更新的应用程序中.我在网上找到的有关数据库升级的教程只强调在应用程序启动时创建的数据库.像这个

Here is the problem: I have a quiz app with an SQlite database in which questions and answers have been populated with SQLite database browser. Now when I add new questions to the database tables and create a new database version, database dont seem to reflect in the updated app. The tutorials I find online on database upgrades enphasizes only on database that is created as application starts. like this

问题:仅当我向 SQLite 中的现有表添加新记录时,如何处理数据库升级?

Question: How do I handle database upgrade only when I add new records to an already existing table in SQLite?

我尝试过的:(1) 在 DatabaseOpenHelper OnUpgrade() 方法中,我删除了添加新记录的表.

What I have tried: (1) in DatabaseOpenHelper OnUpgrade() method, I dropped the tables in which new records were added.

结果: 应用程序崩溃的活动开始

result : App crashed activity starts

(2) 将 OnUpdrade() 方法留空.

(2) Leave the OnUpdrade() method empty.

result : 新添加的记录没有反映

result : The newly added records did not reflect

额外数据:DatabaseOpenHelper OnCreate() 方法也是空的

Extra data: DatabaseOpenHelper OnCreate() method is also empty

代码

@Override
public void onCreate(SQLiteDatabase db) {

}

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

    db.execSQL("drop table if exists "+ DatabaseDescription.LectureNotes.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.QuestionAnswer.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Test.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Subjects.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Topics.TABLE_NAME);
    db.execSQL("drop table if exists "+ DatabaseDescription.Year.TABLE_NAME);
}

推荐答案

预填充的数据库只会从资产文件夹中复制一次.

The pre-populated database will only be copied from the assets folder once.

要使用新版本的预填充数据库进行更新,您需要打开较新更新的数据库的副本,如果现有数据库有需要保留的数据,然后将相关行或表复制到现有数据库中(例如回答问题).

To update with a new version of the pre-populated database you would need to open a copy of the newer updated database and then copy the relevant rows or tables into the existing database if the existing database has data that needs to be kept (e.g. answered questions).

这是一个执行上述操作的示例更新数据库.Sqlite-asset-helper 库

here's an example that does the above Update DB. Sqlite-asset-helper library

这篇关于处理预填充数据库上的 sqlite 数据库升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆