Mind the Flex

Updating the existing Air database

April 5th, 2009 | Posted by Martin
Filed under Adobe Air, Flex, SQLite |

The article offers one possible solution what to do with an existing local databases when you have to change the table structure? Sooner or later you happen to be in an Air project where you have programmed an excellent application that stores all the user related data in a local database. Suddenly, a new excellent idea how to extend the application comes to your mind but the bad thing is that it does not fit into the existing database structure. In case you are in the development phase, you can of course drop the tables and start happily from the beginning but what if a lot of people are already using your application and you don't want to frustrate them with a message that they have to delete the locale database or run manually a database update.

We will create an application where we use a user profile that is stored in a local database. The user profile has at the moment of creation two attributes, gender and age. At some point your boss decides that it is necessary to add also the name attribute to the user profile. As the application is already distributed to thousands of users, you cannot just drop the database. There are multiple classes like SQLSchemaResult that can help you to solve this problem.

The first and simplest one is to just execute an alter table command. Let's suppose that the version 1 of the application looked like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  3. layout="absolute"
  4. creationComplete="creationCompleteHandler()">
  5. <mx:Script>
  6. <![CDATA[
  7. private const DATABASE_NAME:String="updateExample";
  8. private var createProfileTableTxt:String="create table if not exists
  9. userprofile(id integer primary key autoincrement, gender text, age integer);";
  10. private var sqlconnection:SQLConnection;
  11.  
  12. private function creationCompleteHandler():void {
  13. createDatabase();
  14. }
  15.  
  16. private function createDatabase():void {
  17. var file:File=File.userDirectory.resolvePath(DATABASE_NAME);
  18. sqlconnection=new SQLConnection();
  19. sqlconnection.addEventListener(SQLEvent.OPEN, buildDatabaseTables);
  20. sqlconnection.open(file, SQLMode.CREATE);
  21. sqlconnection.loadSchema();
  22. }
  23.  
  24. private function buildDatabaseTables(event:Event):void {
  25. createUserProfileTable();
  26.  
  27. function createUserProfileTable():void {
  28. var stmtCreateUserProfileTable:SQLStatement;
  29. stmtCreateUserProfileTable=new SQLStatement();
  30. stmtCreateUserProfileTable.sqlConnection=sqlconnection;
  31. stmtCreateUserProfileTable.text=createProfileTableTxt;
  32. stmtCreateUserProfileTable.execute();
  33. }
  34. }
  35. ]]>
  36. </mx:Script>
  37. </mx:WindowedApplication>

According to the new business requirement you have to add the name attribute to the existing table. What you will do next is to add the method call alterTableForVersion2 to the end of method createUserProfileTable. All the method alterTableForVersion2 does is querying the database structure from database. By traversing the tables we will find the one we are intersted in and currently is is "userprofile" and by traversing its columns we find that there is no column "name". The last step is the execution of the alter statement which modifies the table so you could add the "name" attribute also to your actionscript objects.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  3. layout="absolute"
  4. creationComplete="creationCompleteHandler()">
  5. <mx:Script>
  6. <![CDATA[
  7. private const DATABASE_NAME:String="updateExample";
  8. private var createProfileTableTxt:String="create table if not exists
  9. userprofile(id integer primary key autoincrement, gender text, age integer);";
  10. private var alterProfileTableTxt:String="alter table userprofile add column name text";
  11. private var sqlconnection:SQLConnection;
  12.  
  13. private function creationCompleteHandler():void {
  14. createDatabase();
  15. }
  16.  
  17. private function createDatabase():void {
  18. var file:File=File.userDirectory.resolvePath(DATABASE_NAME);
  19. sqlconnection=new SQLConnection();
  20. sqlconnection.addEventListener(SQLEvent.OPEN, buildDatabaseTables);
  21. sqlconnection.open(file, SQLMode.CREATE);
  22. }
  23.  
  24. private function buildDatabaseTables(event:Event):void {
  25. createUserProfileTable();
  26.  
  27. function createUserProfileTable():void {
  28. var stmtCreateUserProfileTable:SQLStatement;
  29. stmtCreateUserProfileTable=new SQLStatement();
  30. stmtCreateUserProfileTable.sqlConnection=sqlconnection;
  31. stmtCreateUserProfileTable.text=createProfileTableTxt;
  32. stmtCreateUserProfileTable.execute();
  33. alterTableForVersion2();
  34. }
  35.  
  36. function alterTableForVersion2():void {
  37. sqlconnection.loadSchema();
  38. var schema:SQLSchemaResult=sqlconnection.getSchemaResult();
  39. var nameFieldExists:Boolean=false;
  40.  
  41. for each(var currentTable:SQLTableSchema in schema.tables) {
  42. if (currentTable.name == "userprofile") {
  43. for each(var currentColumn:SQLColumnSchema in currentTable.columns) {
  44. if (currentColumn.name == "name") {
  45. nameFieldExists = true;
  46. }
  47. }
  48. }
  49. }
  50.  
  51. if (!nameFieldExists) {
  52. alterUserProfileTable();
  53. }
  54. }
  55.  
  56. function alterUserProfileTable():void {
  57. var stmtAlterUserProfileTable:SQLStatement;
  58. stmtAlterUserProfileTable=new SQLStatement();
  59. stmtAlterUserProfileTable.sqlConnection=sqlconnection;
  60. stmtAlterUserProfileTable.text=alterProfileTableTxt;
  61. stmtAlterUserProfileTable.execute();
  62. }
  63. }
  64. ]]>
  65. </mx:Script>
  66. </mx:WindowedApplication>

One Response to “Updating the existing Air database”

  1. Updating the existing Air database | Dailytuts.net - Daily tutorial for peoples Says:

    […] Go here to read the rest: Updating the existing Air database […]

Leave a Reply