Data storage and caching with SQLite databases and Adobe AIR
I've made a simple example which is a super simple desktop application which stores it's data in a SQLite database. The idea and implementation here is very similar to what I just described for the caching example. On launch, we first check to see if the database exists. If it does, that means it ought to contain some data so we grab the data and display it. If the database doesn't exist, we create it and add our one default entry, then load it into the application. The user can then add, remove, and update entries. When each of these transactions sends a result of success, we reload all the data in the database. Obviously in a real-world application this wouldn't be a great idea; that's too much overhead. One option would be to just manipulate the dataProvider ArrayCollection after each successful transaction -- but for this simple example I'm leaving it the way it is for the intent of simply demonstrating using SQLite.
At this point I think I'll let the code speak for itself.
ᅠ Basically you'll notice the basic steps are:
1. Create a connection: connection = new SQLConnection();
2. Define the database file: dbFile = File.applicationStorageDirectory.resolvePath(dbFileString);
3. Open (or create and open ) the database: connection.open(dbFile);
4. Create an empty SQLStatement: var sql : SQLStatement = new SQLStatement();
5. Create a query: var sqlString : String = "CREATE TABLE Users (" +
" uid INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT, " +
" phonenumber TEXT)";
6. Attach the connection and the query to the SQLStatement:
sql.sqlConnection = connection;
sql.text = sqlString;
7. Create event listeners for success and failure:
sql.addEventListener(SQLEvent.RESULT, onDBCreateTableResult);
sql.addEventListener(SQLErrorEvent.ERROR, onDBCreateTableError);
8. Execute! : sql.execute()
ᅠ