4

I cant seem to understand how to upgrade the database after reading the docs at 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 :
1) get ArrayList of user defined data from table 1
2) update database
3) put the user data back into table 1

Am I thinking about this all wrong?
Any tips would be greatly appreciated.

4 Answers 4

2

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.

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";
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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.

2 Comments

I did consider that, but it is a couple thousand rows and what if the user misses this upgrade and upgrades to version 3. I would like to know how to retain user data in tables for other instances.
Also.. onUpdate is used by the SQLiteAssetHelper, if you overwrite it, it does not do its magic.
0

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. To ship: click,
To remove:

this.deleteDatabase("<yourdatabasename>");

For example;

this.deleteDatabase("db.db");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.