Adobe AIR includes the capability of creating and working with local SQL databases. Many stand SQL features are supported in the runtime, open source SQLite system can be used for storing local, persistent data.
The flollowing is a simplistic example that create a sqlite database, add, get, update and remove records from the “user” table.
Notice:
You might have wondered about this line:
- sqlConnection.openAsync(dbFile);
.Asychnronous means that your code will have an event listener on the SQLConnection and an event handler for the response.
.Synchronous means that your application will make an “inline” call to SQLite where it performs the operation and then moves on as if it were any other line of actionscript code.This tutorial is using the asynchronous method.
- <?xml version=”1.0? encoding=”utf–8??>
- <mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
- preinitialize=”openDatabaseConnection()”
- fontSize=”12? backgroundColor=”#FFFFFF” width=”600? height=”700?>
- <mx:Script>
- <![CDATA[
- import flash.data.SQLConnection;
- import flash.events.SQLErrorEvent;
- import flash.events.SQLEvent;
- import flash.filesystem.File;
- private var conn:SQLConnection;
- private var initComplete:Boolean = false;
- private var sqlStat:SQLStatement;
- public function openDatabaseConnection():void{
- // create new sqlConnection
- sqlConnection = new SQLConnection();
- sqlConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
- sqlConnection.addEventListener(SQLErrorEvent.ERROR, errorHandler);
- // get currently dir
- var dbFile:File = File.applicationStorageDirectory.resolvePath(“sampleDB.db“);
- // open database,If the file doesn’t exist yet, it will be created
- sqlConnection.openAsync(dbFile);
- }
- // connect and init database/table
- private function onDatabaseOpen(event:SQLEvent):void
- {
- // init sqlStatement object
- sqlStat = new SQLStatement();
- sqlStat.sqlConnection = conn;
- var sql:String = “CREATE TABLE IF NOT EXISTS user (“ +
- “ id INTEGER PRIMARY KEY AUTOINCREMENT, “ +
- “ name TEXT, “ +
- “ password TEXT“ +
- “)“;
- sqlStat.text = sql;
- sqlStat.addEventListener(SQLEvent.RESULT, statResult);
- sqlStat.addEventListener(SQLErrorEvent.ERROR, createError);
- sqlStat.execute();
- }
- private function statResult(event:SQLEvent):void
- {
- // refresh data
- var sqlresult:SQLResult = sqlStat.getResult();
- if(sqlresult.data == null){
- getResult();
- return;
- }
- datafiled.dataProvider = sqlresult.data;
- }
- // get data
- private function getResult():void{
- var sqlquery:String = “SELECT * FROM user“
- excuseUpdate(sqlquery);
- }
- private function createError(event:SQLErrorEvent):void
- {
- trace(“Error code:“, event.error.code);
- trace(“Details:“, event.error.message);
- }
- private function errorHandler(event:SQLErrorEvent):void
- {
- trace(“Error code:“, event.error.code);
- trace(“Details:“, event.error.message);
- }
- // update
- private function excuseUpdate(sql:String):void{
- sqlStat.text = sql;
- sqlStat.execute();
- }
- // insert
- private function insertemp():void{
- var sqlupdate:String = “Insert into user(id,name,password) values(‘“ +
- name.text +
- “‘,’“ +
- password.text +
- “‘)“;
- debug.text+=sqlupdate+“\n“
- excuseUpdate(sqlupdate)
- }
- // delete
- private function deleteemp():void{
- var sqldelete:String = “delete from user where id=’“ +
- datafiled.selectedItem.id +
- “‘“;
- excuseUpdate(sqldelete);
- debug.text+=sqldelete+“\n“
- }
- ]]>
- </mx:Script>
- <mx:TextArea x=”21? y=”10? width=”402? height=”179? id=”debug”/>
- <mx:DataGrid x=”21? y=”197? id=”datafiled”>
- <mx:columns>
- <mx:DataGridColumn headerText=”ID” dataField=”id”/>
- <mx:DataGridColumn headerText=”name” dataField=”name”/>
- <mx:DataGridColumn headerText=”password” dataField=”password”/>
- </mx:columns>
- </mx:DataGrid>
- <mx:Form x=”21? y=”471?>
- <mx:FormItem label=”name”>
- <mx:TextInput id=”name”/>
- </mx:FormItem>
- <mx:FormItem label=”password”>
- <mx:TextInput id=”password”/>
- </mx:FormItem>
- </mx:Form>
- <mx:Button x=”300? y=”503? label=”add” click=”insertemp()”/>
- <mx:Button x=”300? y=”533? label=”delete” click=”deleteemp()”/>
- </mx:WindowedApplication>