Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Contributor
culebrins
Posts: 4
Registered: ‎05-22-2011
My Device: Blackberry Playbook
Accepted Solution

Force script during installation

Hi guys,

 

I need to launch a new version of my app to solve some bugs. To solve one of them I need to launch an script or launch a process to update some fields of the database that it's used by the app.

 

Do you know if I can force an update of the database when someone updates my app? Is there any way to do that?

 

thanks!

Please use plain text.
Developer
JohnPinkerton
Posts: 350
Registered: ‎01-21-2011
My Device: Curve 8900 (Personal) / Bold 9650 (Work)

Re: Force script during installation

So in my app (Network Manager) I ran into the same situation as you. I needed to add support for additional fields/change the database structure after release.

 

Quick rundown of how I did it:

Saved a number in local storage representing the installedDBVersion.

When I need to do a database update, I increment the currentDBVersion.

When my SQL class is instantiated, it checks to see if currentDBVersion > installedDBVersion. If it is an update is triggered.

 

I create a new class for each database update I do. I've set it up to have a message on screen informing the user that a database update is needed. It waits for them to click Start. Once they do a backup of the existing database is created, the new columns/tables are added, then the installedDBVersion gets updated accordingly.

 

The user is notified the app needs to restart for changes to be complete. They only have an option to Exit, as we can't "restart" the app so to speak.

 

This process has worked well for me so far, I've only had to do it twice, but I keep all the database updates in the app, and intend to combine them as is possible/necessary to reduce the number of times someone might need to run the update process should they skip one (or more) updates from App World.

 

Hope this helps!

Please use plain text.
New Contributor
culebrins
Posts: 4
Registered: ‎05-22-2011
My Device: Blackberry Playbook

Re: Force script during installation

Thanks for your fast response...

 

One question: what do you mean with "Saved a number in local storage"? and where is saved the "currentDBVersion"? Also in local storage?

 

Thanks!

Please use plain text.
Developer
JohnPinkerton
Posts: 350
Registered: ‎01-21-2011
My Device: Curve 8900 (Personal) / Bold 9650 (Work)

Re: Force script during installation

[ Edited ]

I just typed this up quick, it should help you get going down the right path. You'll need to tweak it accordingly for your project though :smileywink:

 

//Define our variables
private var dbVSO:SharedObject = SharedObject.getLocal("DBVersion"); //Persist the database version
private var installedDBVersion:Number = dbVSO.data.dbV; //Pull the database version from localstorage
private var currentDBVersion:Number = 2; //Used to check in the event of an app upgrade
private var dbFile:File = File.applicationStorageDirectory.resolvePath("sweetDB.db");

//Check if we need to create dbFile:
if(!dbFile.exists) {
	createDB();
} else {
	checkUpdates();
}



//Put this in checkUpdates function.
//Check if the database is already created, but localstorage hasn't been setup...
if(installedDBVersion == undefined) {
	dbVSO.data.dbV = 2; //Current install considered version 1
	dbVSO.flush(); //Saves the information to localstorage
	installedDBVersion = dbVSO.data.dbV; //Updates our variable to reflect the correct status

	//Run the update function
	version2Update();
	return; //We don't want to go any further.
}

//Check if the database version currently running is 
if(installedDBVersion < currentDBVersion) {
	//We need to do a database update.
}


/*
	You're probably making the additions to your database now. Meaning fresh installs will get the
	latest version of the database, so you won't need to run the update on them. Only existing users.
*/

//In your createDB function you'll want to handle setting the localstorage up:
private function createDB():void {
	dbVSO.data.dbV = 2; //Current installs out there would be version 1?
	dbVSO.flush(); //Saves the information to localstorage
	installedDBVersion = dbVSO.data.dbV; //Updates our variable to reflect the correct status

	//Rest of your SQL database creation stuff....
	// ...
}

 

Here's a pastebin if it makes it easier to read: http://pastebin.com/f2siDhfD

Please use plain text.
New Contributor
culebrins
Posts: 4
Registered: ‎05-22-2011
My Device: Blackberry Playbook

Re: Force script during installation

Thanks! very helpfull!

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Force script during installation

Another way is to have each database file have an "info" table that contains data schema version (and other fields). It wouuld just have one record. Then the information about the database files stays with the database file.
Please use plain text.
Developer
JohnPinkerton
Posts: 350
Registered: ‎01-21-2011
My Device: Curve 8900 (Personal) / Bold 9650 (Work)

Re: Force script during installation

I might be misunderstanding, are you talking then about having multiple database files (file for each "version")? Wouldn't you then have to move the data from each file?
Please use plain text.
Developer
JohnPinkerton
Posts: 350
Registered: ‎01-21-2011
My Device: Curve 8900 (Personal) / Bold 9650 (Work)

Re: Force script during installation

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Force script during installation

No, just a table called "info" in the database file. I have it with one field named "version". I access this table when the database is opened. If the version is less than what is expected, then I run a routine to make the appropriate changes to the the other tables that might be in it. It works pretty well and is self contained.
Please use plain text.