Using the local database with Adobe AIR

We often need our system can store data locally without the need for a robust database as a MySQL or SQL Server, or even that data can be stored locally for later synchronization with a remote database. Adobe AIR offers the possibility of working with database locally, your bank is based on the SQLite database, the more used to this kind of need to be very fast and easy to work with. Was added transaction support in Adobe AIR 2.0.
Let’s do a simple example, start with the SQLConnection class, she will be responsible for connecting with our local bank, it is also defined that the database will be used only in memory or whether it created a local file (extension. db).
[ACTIONSCRIPT3]
conn = new SQLConnection();
try
{
conn.open(dbFile);//conn.open(null) passe null para o banco ser criado somente em memoria
}
catch(err:Error)
{
trace(ObjectUtil.toString(err));
return;
}
[/ACTIONSCRIPT3]
In this case, I am creating a local file to the database. dbfiles is an instance of the File class:
[ACTIONSCRIPT3]
dbFile = File.applicationStorageDirectory.resolvePath(“dbFile.db”);
[/ACTIONSCRIPT3]
Having created our database, we create our table for storing data. We will use the class SQLStatement.
[ACTIONSCRIPT3]
createStmt = new SQLStatement();
createStmt.sqlConnection = conn;
[/ACTIONSCRIPT3]
We will pass by the SQL string to create the table then to be performed by AIR:
[ACTIONSCRIPT3]
var sql:String = “”;
sql += “CREATE TABLE IF NOT EXISTS contato (“;
sql += ” id INTEGER PRIMARY KEY AUTOINCREMENT,”;
sql += ” nome TEXT,”;
sql += ” email TEXT”;
sql += “)”;
createStmt.text = sql;
try
{
createStmt.execute();
}
catch(error:SQLError)
{
trace(“CREATE TABLE error:”, error);
trace(“error.message:”, error.message);
trace(“error.details:”, error.details);
return void;
}
[/ACTIONSCRIPT3]
We have our database and tables created, let’s create a form to enter some data in the database:
[ACTIONSCRIPT3]
insertStmt = new SQLStatement();
insertStmt.sqlConnection = conn;
var sql:String = “”;
sql += “INSERT INTO contatos (nome, email) “;
sql += “VALUES (‘”+txtNome.text+”‘, ‘”+txtEmail.text+”‘)”;
insertStmt.text = sql;

try
{
insertStmt.execute();
}
catch (error:SQLError)
{
trace(“INSERT error:”, error);
trace(“error.message:”, error.message);
trace(“error.details:”, error.details);
return void;
}
[/ACTIONSCRIPT3]
Now just create the method to execute the select
[ACTIONSCRIPT3]
selectStmt = new SQLStatement();
selectStmt.sqlConnection = conn;
var sql:String = “SELECT id, nome, email FROM contatos”;
selectStmt.text = sql;

try
{
selectStmt.execute();
}
catch (error:SQLError)
{
trace(“SELECT error:”, error);
trace(“error.message:”, error.message);
trace(“error.details:”, error.details);
return;
}

var result:SQLResult = selectStmt.getResult();
dg.dataProvider = result.data;
[/ACTIONSCRIPT3]
Following is the complete code:
[MXML]





































[/MXML]

more:
Quick Start: Working asynchronously with a local SQL database (Flex)
Quick Start: Working asynchronously with a local SQL database (Flash)
Quick Start: Working asynchronously with a local SQL database (HTML)
Quick Start: Working synchronously with a local SQL database (Flex)
Quick Start: Working synchronously with a local SQL database (Flash)
Quick Start: Working synchronously with a local SQL database (HTML)

Books:
Adobe AIR Bible (Bible (Wiley))
Adobe AIR in Action

Was this article helpful? feel free to make a donation and help keep the blog in the air
ActionScript 3.0, Adobe AIR, Flex , ,

4 comments


  1. Pingback: Flex learner | Blog | Leonardo França » Using the local database with Adobe AIR

  2. Pingback: Protect your computer and personal information by using an anonymous proxy server « e377

  3. Pingback: » Stress-Free Zen Cart Product Import – Know-How

Leave a Reply