Super-simple SQLite example for Adobe AIR 1 Beta
by Carlos Pinho, June 15th, 2007
  • Share
  • Share

Sean wrote:

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();
}
}

Original article here.

Brgds,
CP

Copy and Paste the code below
Email and IM
Websites
Forums
Get This

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

  1. Blue Says:

    Sounds great…but the zip file link appears to be invalid.

  2. cpinho Says:

    I tested the link and it was ok! Dunno what happened! Try again and tell me pls!!!

  3. Blue Says:

    Sorry, my fault. I got confused by the WordPress snapshots pop-up box. I didn’t realize that you can’t click the link directly. Instead, you have to click the link that appears as the title in the pop-up box.

    Thanks for the quick response…and, of course, for all your effort in writing and posting it. I look forward to trying this out!

  4. SQLite example for Adobe AIR | Adobe AIR Tutorials Says:

    [...] more @ http://flashenabled.wordpress.com/2007/06/15/super-simple-sqlite-example-for-adobe-air-1-beta/ Source code for download [...]

  5. Ryan Says:

    Okay, if you’re going to have a little tiny microscopic smiley face at the top of your site I want a freaking easter egg if I click on it 150 times, that’s just web etiquette!!!

Let leave a Comments for this post.