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>
I am sorry, this code does not work.
First of all, the SQLConnection is introduced as “private var conn:SQLConnection;”, but than its name is changed to “sqlConnection”.
id=”name” is not valid for a TextInput; I changed it to “username”.
Than I upgraded all the question marks that should be ” instead so that the code is without errors – but still it does not work:
Whenever I start the application and try to add a username/password, the application crashes with the following error message:
Error: Error #3106: Property cannot be changed while SQLStatement.executing is true.
at Error$/throwError()
at flash.data::SQLStatement/set text()
at DataBase/excuseUpdate()[C:\Dokumente und Einstellungen\eachter\eachter@Y_localCopy\Diplomarbeit6_dev\Flex\Flex Builder workspace_BAK_2008-08-13_am_OpenSync\DataBase\src\DataBase.mxml:72]
at DataBase/insertemp()[C:\Dokumente und Einstellungen\eachter\eachter@Y_localCopy\Diplomarbeit6_dev\Flex\Flex Builder workspace_BAK_2008-08-13_am_OpenSync\DataBase\src\DataBase.mxml:83]
at DataBase/___DataBase_Button1_click()[C:\Dokumente und Einstellungen\eachter\eachter@Y_localCopy\Diplomarbeit6_dev\Flex\Flex Builder workspace_BAK_2008-08-13_am_OpenSync\DataBase\src\DataBase.mxml:111]
Do you have working code, maybe in a zip file?
Regards, Christian