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).
-
conn = new SQLConnection();
-
try
-
{
-
conn.open(dbFile);//conn.open(null) passe null para o banco ser criado somente em memoria
-
}
-
{
-
trace(ObjectUtil.toString(err));
-
return;
-
}
In this case, I am creating a local file to the database. dbfiles is an instance of the File class:
-
dbFile = File.applicationStorageDirectory.resolvePath("dbFile.db");
Having created our database, we create our table for storing data. We will use the class SQLStatement.
-
createStmt = new SQLStatement();
-
createStmt.sqlConnection = conn;
We will pass by the SQL string to create the table then to be performed by AIR:
-
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;
-
}
We have our database and tables created, let's create a form to enter some data in the database:
-
insertStmt = new SQLStatement();
-
insertStmt.sqlConnection = conn;
-
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;
-
}
Now just create the method to execute the select
-
selectStmt = new SQLStatement();
-
selectStmt.sqlConnection = conn;
-
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;
Following is the complete code:
-
<?xml version="1.0" encoding="utf-8"?>
-
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
-
xmlns:s="library://ns.adobe.com/flex/spark"
-
xmlns:mx="library://ns.adobe.com/flex/mx">
-
<fx:Script>
-
<![CDATA[
-
import flash.data.SQLConnection;
-
import flash.data.SQLResult;
-
import flash.data.SQLStatement;
-
import flash.errors.SQLError;
-
import flash.events.MouseEvent;
-
import flash.filesystem.File;
-
-
import mx.utils.ObjectUtil;
-
-
private var conn:SQLConnection;
-
private var dbFile:File;
-
private var createStmt:SQLStatement;
-
private var createStatusStmt:SQLStatement;
-
private var insertStmt:SQLStatement;
-
private var selectStmt:SQLStatement;
-
-
override protected function childrenCreated():void
-
{
-
super.childrenCreated();
-
-
dbFile = File.applicationStorageDirectory.resolvePath("dbContatos.db");
-
conn = new SQLConnection();
-
-
try
-
{
-
conn.open(dbFile);
-
}
-
catch(err:Error)
-
{
-
trace(ObjectUtil.toString(err));
-
return;
-
}
-
-
createTable();
-
-
btnInsert.addEventListener(MouseEvent.CLICK,addData);
-
}
-
-
private function createTable():void
-
{
-
createStmt = new SQLStatement();
-
createStmt.sqlConnection = conn;
-
-
var sql:String = "";
-
sql += "CREATE TABLE IF NOT EXISTS contatos (";
-
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;
-
}
-
-
getData();
-
}
-
-
private function addData(evt:MouseEvent=null):void
-
{
-
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;
-
}
-
getData();
-
}
-
-
private function getData():void
-
{
-
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;
-
}
-
-
]]>
-
</fx:Script>
-
<fx:Declarations>
-
<!-- Place non-visual elements (e.g., services, value objects) here -->
-
</fx:Declarations>
-
<s:Group width="100%" height="100%">
-
<mx:ViewStack x="0" y="68" id="viewstack1" width="100%" height="198" creationPolicy="all">
-
<s:NavigatorContent label="Registros" width="100%" height="100%">
-
<mx:DataGrid id="dg" x="18" y="30" width="100%" height="100%">
-
<mx:columns>
-
<mx:DataGridColumn headerText="ID" width="25" dataField="id"/>
-
<mx:DataGridColumn headerText="Nome" width="200" dataField="nome"/>
-
<mx:DataGridColumn headerText="Email" width="200" dataField="email"/>
-
</mx:columns>
-
</mx:DataGrid>
-
</s:NavigatorContent>
-
<s:NavigatorContent label="Inserir Registro" width="100%" height="100%">
-
<s:Label x="10" y="10" text="Inserir Registro"/>
-
<mx:Form x="10" y="34" width="100%" height="100%">
-
<mx:FormItem label="Nome:">
-
<s:TextInput id="txtNome"/>
-
</mx:FormItem>
-
<mx:FormItem label="Email:">
-
<s:TextInput id="txtEmail"/>
-
</mx:FormItem>
-
<mx:FormItem>
-
<s:Button id="btnInsert" label="Inserir"/>
-
</mx:FormItem>
-
</mx:Form>
-
</s:NavigatorContent>
-
</mx:ViewStack>
-
<mx:LinkBar x="0" y="35" dataProvider="{viewstack1}">
-
</mx:LinkBar>
-
</s:Group>
-
</s:WindowedApplication>
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

Pingback: Flex learner | Blog | Leonardo França » Using the local database with Adobe AIR
Pingback: Protect your computer and personal information by using an anonymous proxy server « e377
Pingback: » Stress-Free Zen Cart Product Import – Know-How
Thanks for sharing this very clear tutorial off Adobe AIR I Learn so much on this.