简体   繁体   English

Android:使用SQLiteAssetHelper升级用户数据库

[英]Android : Upgrading user database using SQLiteAssetHelper

I cant seem to understand how to upgrade the database after reading the docs at https://github.com/jgilfelt/android-sqlite-asset-helper https://github.com/jgilfelt/android-sqlite-asset-helper阅读文档后,我似乎无法理解如何升级数据库

I want to keep the users data in one table only. 我想将用户数据仅保留在一个表中。 I don't need to alter any tables. 我不需要改变任何表格。 The upgrade is pretty much adding new rows in another table. 升级几乎是在另一个表中添加新行。

So the flow (I'm guessing) would be : 所以流程(我猜)是:

onUpgrade : onUpgrade:
1) get ArrayList of user defined data from table 1 1)从表1中获取用户定义数据的ArrayList
2) update database 2)更新数据库
3) put the user data back into table 1 3)将用户数据放回表1中

Am I thinking about this all wrong? 我觉得这一切都错了吗?
Any tips would be greatly appreciated. 任何提示将非常感谢。

I misunderstood the point of the update script. 我误解了更新脚本的要点。
When your app is updated it does not install the new database. 当您的应用程序更新时,它不会安装新数据库。 It keeps your old one and only runs the update script to bring your old one up to speed. 它保留了旧的,只运行更新脚本,使您的旧脚本加快速度。

So if you dont need to change your user data it will not be lost. 因此,如果您不需要更改您的用户数据,它将不会丢失。 It will only run the sql in the script. 它只会在脚本中运行sql。

If you do need to alter your user data table you can use a temporary table to do the change. 如果确实需要更改用户数据表,可以使用临时表进行更改。

eg : 例如:

-- add a FullNames column to Employees
ALTER TABLE "Employees" RENAME TO 'Employees_ME_TMP';

CREATE TABLE "Employees" 
(
    "EmployeeID" int NOT NULL,
    "LastName" varchar(20) NOT NULL,
    "FirstName" varchar(10) NOT NULL,
    "FullName" varchar(150),
    PRIMARY KEY ("EmployeeID")
);

INSERT INTO "Employees"  
(
    "EmployeeID", 
    "LastName", 
    "FirstName", 
    "FullName"
) 
SELECT 
    "EmployeeID", 
    "LastName", 
    "FirstName", 
    "FirstName" || ' ' || "LastName" 
FROM 
    "Employees_ME_TMP";

DROP TABLE "Employees_ME_TMP";

I want to keep the users data in one table only. 我想将用户数据仅保留在一个表中。 I don't need to alter any tables. 我不需要改变任何表格。 The upgrade is pretty much adding new rows in another table. 升级几乎是在另一个表中添加新行。

If you just want to add some rows, then just add them in onUpdate. 如果您只想添加一些行,那么只需在onUpdate中添加它们即可。

There is an easier way that I found. 我找到了一种更简单的方法。 After updating database in the main/assets/databases folder by using Firefox SQLite Manager, just remove the previous database firstly before creating the database in the code , then ship database to application again. 使用Firefox SQLite Manager更新main / assets / databases文件夹中的数据库后,在代码中创建数据库之前先删除以前的数据库 ,然后再将数据库发送到应用程序。 To ship: click , 发货: 点击
To remove: 去除:

this.deleteDatabase("<yourdatabasename>");

For example; 例如;

this.deleteDatabase("db.db");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

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

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