Super-simple SQLite example for Adobe AIR 1 Beta

Here is a very simplistic Flex Builder 3 and AIR example that adds and removes records from a table in a local SQLite database. This is not a fully completed AIR project but this code can probably be reused by anyone wanting to get started with AIR+SQLite. There are a couple of other AIR+SQLite examples out there to check out and don't forget the documentation on the adobe site.

Installer:
AIRSQLiteExample.air

Flex 3 Project files (source):
AIRSQLiteExample.zip


/*
* Super-simple SQLite example for Adobe AIR 1 Beta
* 06/14/07
* Sean Moore
* seantheflashguy@gmail.com
* seantheflashguy.com/blog
*/

import flash.data.SQLResult;
import flash.filesystem.File;
import flash.data.SQLStatement;
import flash.data.SQLConnection;
import flash.events.SQLEvent;
import flash.events.SQLErrorEvent;

import User;

private var exampleDB:SQLConnection;
private var exampleDBFile:File;
private var dbStatement:SQLStatement;

[Bindable]
private var userData:Array;

private function init():void
{
    initAndOpenDatabase();
}

private function initAndOpenDatabase():void
{
    exampleDBFile = new File("app-resource:/ExampleDatabase.db");
    exampleDB = new SQLConnection();
    exampleDB.addEventListener(SQLEvent.OPEN, onExampleDBOpened);
    exampleDB.addEventListener(SQLErrorEvent.ERROR, onExampleDBError);
    exampleDB.open(exampleDBFile);
}

private function onExampleDBOpened(event:SQLEvent):void
{
    if (event.type == "open")
    {
        getRecords();
    }
}

private function onExampleDBError(event:SQLEvent):void
{
}

private function getRecords():void
{
    dbStatement = new SQLStatement();
    dbStatement.itemClass = User;
    dbStatement.sqlConnection = exampleDB;
    var sqlQuery:String = "select * from Users";
    dbStatement.text = sqlQuery;
    dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementSelectResult);
    dbStatement.execute();
}

private function onDBStatementSelectResult(event:SQLEvent):void
{
    var result:SQLResult = dbStatement.getResult();
    if (result != null)
    {
        userData = result.data;
    }
}

private function onDBStatementInsertResult(event:SQLEvent):void
{
    if (exampleDB.totalChanges >= 1)
    {
        getRecords();
    }
}

private function addUserToDatabase(user:User):void
{
    var sqlInsert:String = "insert into Users values('"+user.firstname+"','"+user.lastname+"','"+user.email+"');";
    dbStatement.text = sqlInsert;
    dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementSelectResult);
    dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertResult);
    dbStatement.execute();
}

private function onAddUserButtonClicked(event:MouseEvent):void
{
    var user:User = new User();
    user.firstname = firstNameTextInput.text;
    user.lastname = lastNameTextInput.text;
    user.email = emailTextInput.text;
    addUserToDatabase(user);
}

private function onUsersDataGridChanged(event:Event):void
{
}

private function onRemoveUserButtonClicked(event:MouseEvent):void
{
    removeUserFromDatabase(usersDataGrid.selectedItem as User);
}

private function removeUserFromDatabase(user:User):void
{
    var sqlDelete:String = "delete from Users where firstname='"+
                                  user.firstname+"' and lastname='"+
                                  user.lastname+"' and email='"+user.email+"';";
    trace("sqlDelete="+sqlDelete);
    dbStatement.text = sqlDelete;
    dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementInsertResult);
    dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementDeleteResult);
    dbStatement.execute();
}

private function onDBStatementDeleteResult(event:SQLEvent):void
{
    if (exampleDB.totalChanges >= 1)
    {
        getRecords();
    }
}

23 Responses to “Super-simple SQLite example for Adobe AIR 1 Beta”

  1. Looks like the Downloadlinks are broken.
    But good example!

  2. Your zip link is broken. It is missing the .com.

  3. [...] heißt ja nun AIR und unterstützt SQL. Hier gibt es ein kleines Beispiel [...]

  4. [...] Original post here. [...]

  5. [...] Super-simple SQLite example for Adobe AIR 1 Beta by Sean Moore: ActionScript Coder (tags: adobe sqlite google gears apollo air database) [...]

  6. I get a runtime error — “Illegal type coercion”.
    The statement:
    private function onExampleDBError(event:SQLEvent):void
    Should read:
    private function onExampleDBError(event:SQLErrorEvent):void

  7. Correct url:

    http:///www.seantheflashguy.com/air/AIRSQLiteExample.zip

  8. http://www.seantheflashguy.com/air/AIRSQLiteExample.zip

  9. the link for the zip is broken

  10. Wow! This is really really great. I’m pretty new to Flex/AS and just started to investigate into AIR and your application has helped me with all these technologies! I’m curious.. do you use RemoteObject at all? How would one implement it for online usage but when offline cache to sqlite (or something) and then upload/sync. I have a few ideas but would love to discuss it further.

    Thanks again, this was truly a great (functional, easy to understand, bug-free) code example.

    Regards,
    Mark S.
    AIM:SkramX | Gtalk:marks.skram
    SkramX/Skram on Freenode/EFNet

  11. I would like to read some more documentation and examples about using
    AIRSQLite. Can you help me by giving me some links? Thank you very much:).

  12. hi, i have a question?
    How I can replication data with adobe Air to a Database SQLServer 2005 ¿?

    thanks

    regards

  13. thanks for the GREAT post! Very useful…

  14. [...] Read more @ http://seantheflashguy.com/blog/2007/06/14/super-simple-sqlite-example-for-adobe-air-1-beta/ [...]

  15. nice tutorial.
    one question only..
    Lets say i have a static table with monthnames in it.
    How do i create that?

    in php/mysql you can say values(rule1),(rule2),(rule3)
    to insert multiple records into the database.

    How do i get this working in air/sqlite?

    Using the following lines over and over again works.. but leaves a whole lot of code.. is there an easy way?

    var sqlInsert:String = “insert into Users values(’”+user.firstname+”‘,’”+user.lastname+”‘,’”+user.email+”‘);”;
    dbStatement.text = sqlInsert;
    dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementSelectResult);
    dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertResult);
    dbStatement.execute();

    var sqlInsert2:String = “insert into Users values(’”+user.firstname+”‘,’”+user.lastname+”‘,’”+user.email+”‘);”;
    dbStatement.text = sqlInsert2;
    dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementSelectResult);
    dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertResult);
    dbStatement.execute();

    Greets,J.

  16. [...] easier) Peter Ent (Like progressive development of Pete’s Atmosphere Music Player) Sean the Flash (Flex) Guy’s super simple SQLite example (AIR Beta 1, but should work OK with minor tweaks) Sean also has a series starting about 6 AIR APIs [...]

  17. I’m trying to find information about launching an application from inside an air application. I can’t get AIR to launch it. In Flash you can launch another application using fscommand.exec. But I use SQLLite and filsystem which are not available in a projector file created from Flash. Do you have any ideas?

  18. The installer does not work with the first realease version of Air. It’s a beta (Apollo) app. Will this post be updated?

  19. Severity and Description Path Resource Location Creation Time Id
    Type was not found or was not a compile-time constant: data. [Generated code (use -keep to save): Path: data-generated.as, Line: 345, Column: 14] data Unknown 1207057645250 18

    THIS WAS THE ERROR WHEN I RUN YOUR APPLICATION

  20. [...] Super-simple SQLite example for Adobe AIR 1 Beta - [...]

  21. [...] Super-simple SQLite example for Adobe AIR 1 Beta - [...]

  22. [...] Super-simple SQLite example for Adobe AIR 1 Beta - [...]

  23. [...] Super-simple SQLite example for Adobe AIR 1 Beta - [...]

Leave a Reply