简体   繁体   English

如何使用SQLiteAssetHelper用新行升级数据库版本

[英]how to upgrade database version with new rows with SQLiteAssetHelper

i have developed a tutorial app in which i have saved more than 500 questions and answers. 我开发了一个教程应用程序,其中保存了500多个问题和答案。 their is one more table called favourites. 他们又是一张被称为“最爱”的桌子。 which is for user input. 供用户输入。 now, i want to update my app with new questions and answers. 现在,我想用新的问题和答案更新我的应用程序。 but i dont want to erase the data of favourites table (in case, user has marked some questions favourites, so those questions should not be erased from favourites) 但是我不想删除“收藏夹”表中的数据(以防用户标记了一些收藏夹中的问题,因此不应从收藏夹中删除那些问题)

so how can i do it? 那我该怎么办呢? because, i have used SQLassethelper library for database connectivity. 因为,我已经使用SQLassethelper库进行数据库连接。


my old db contains: 我的旧数据库包含:

  1. data table(static table) 数据表(静态表)
  2. favourites table(local table) 收藏夹表(本地表)

so, according to sqliteassethelper documentation i added my new db: that contains: updated data table. 因此,根据sqliteassethelper文档,我添加了新的db:,其中包含:更新的数据表。 i didnt inserted favourites table here coz it will be created in script file. 我没有在这里插入收藏夹表,因为它将在脚本文件中创建。 and stored that db in assets>>databases folder. 并将该数据库存储在assets >> databases文件夹中。

then i created a script fild db.db_upgrade_1-2.sql 然后我创建了一个脚本fild db.db_upgrade_1-2.sql

alter table "favourites" rename to "favourites_tmp";
create table "favourites" (
"id" Integer not null primary key autoincrement unique,
"question" text,
"answer" text,
"category" text,
"catid" integer
);
insert into "favourites" ("id","question","answer","category","catid") select from "favourites_tmp" "id","question","answer","category","catid" from "favourites_tmp";
drop table "favourites_tmp";

so i think here favourites table will be created with old data. 所以我认为这里的收藏夹表将使用旧数据创建。 but when i run the project, it says: no such tabld favourites. 但是当我运行该项目时,它说:没有这样的表格收藏。

The documentation tells you to 文档告诉您

create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version. 创建一个包含所有必需的SQL命令的文本文件,以将数据库从以前的版本升级到当前版本。

You can use any SQL commands, not only ALTER TABLE, but also INSERT. 您可以使用任何 SQL命令,不仅可以使用ALTER TABLE,还可以使用INSERT。

The database file in the assets folder is used only if there is no old data (if the app is installed for the first time). 只有在没有旧数据的情况下(如果是首次安装该应用程序),才会使用资产文件夹中的数据库文件。 If there is old data, SQLiteAssetHelper executes the SQL upgrade script instead. 如果有旧数据,则SQLiteAssetHelper执行SQL升级脚本。

The SQLiteAssetHelper project contains an example that shows how such a script would look like. SQLiteAssetHelper项目包含一个示例该示例显示此类脚本的外观。

To keep the data in the favourites table, you do not need to do anything. 要将数据保留在收藏夹表中,您无需执行任何操作。 To add the new question/answers, use a bunch of INSERT statements. 要添加新的问题,请使用一串INSERT语句。 (For how to get those INSERT statements, see How to compare two SQLite databases .) (有关如何获取这些INSERT语句的信息,请参见如何比较两个SQLite数据库 。)

finally i got answer for my questions. 终于我得到了我的问题的答案。 as i said i am using SQLiteAssetHelper library. 如我所说,我正在使用SQLiteAssetHelper库。 and now i want to add new records and want to release the update version. 现在我想添加新记录并想要发布更新版本。 so i was finding the feature by which i can store new updated db in assets folder. 所以我在寻找可以将新的更新数据库存储在Assets文件夹中的功能。 and this library will update the old db in user's phone. 该库将更新用户电话中的旧数据库。 with keeping particular table in old db. 与保留旧数据库中的特定表。

but currently this library doesnt offers such feature. 但目前该库不提供此类功能。 the upgrade script can be use only to make changes is old db. 升级脚本只能用于更改旧数据库。 we can add new updated db and copy the data from new one. 我们可以添加新的更新的数据库,并从新数据库复制数据。

if we want to add new data and also dont want to erase local data table. 如果我们要添加新数据,也不想删除本地数据表。 then we need to write insert queries in upgrade script as described in documentation of that library. 那么我们需要按照该库的文档中所述在升级脚本中编写插入查询。

but if we have to add 100 queries then their is no shortcut for it. 但是如果我们必须添加100个查询,那么它们就不是捷径了。 we have to write 100 insert statements in upgrade script. 我们必须在升级脚本中编写100条插入语句。

and in case if we gate a error saying cannot upgrade read only database from version x to y then here is the solution for it: 如果我们出现错误消息说无法将只读数据库从版本x升级到y,那么这里是解决方案:

SQLITE cant upgrade read-only database from version 1 to 2 SQLITE无法将只读数据库从版本1升级到版本2

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

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