Adobe AIR and SQLite Connectivity
===========================
Abode AIR provides very easy mechanism to connect to the SQLite.I am doing the connectivity by coding. Here nothing different just create the connection and create the SQLConnection,open it, create SQLStatement and execute them, close connection.Upto my experience I think this is the same common way which we used in any database connectivity. So there is nothing special. I found this process very cool especially due the portable size of the SQLite and creating and deleting the database in just one mouse click. Apart form this you don’t require any thing else Flex Builder IDE with AIR. There is no software requirement for the SQLite.
I am just putting some line of code for your better understanding. You can just copy and paste it. Although Its just a small code but hope it will give you the over all idea about dealing with the database in an AIR application.
*************************************************************
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”vertical” creationComplete=”init()”
width=”500” height=”400” backgroundColor=”#000000“>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import flash.data.*;
private var ob:Array=new Array();
public var mysqlConnection:SQLConnection;
public var _createProdct:SQLStatement;
public var _insertProduct:SQLStatement;
public var _selectProduct:SQLStatement;
private const _counterProduct:int = 1;
private var databaseFile:File =
File.applicationStorageDirectory.resolvePath("data.db");
private const CREATE_PRODUCT_TABLE:String =
"CREATE TABLE product (id INTEGER PRIMARY KEY,name TEXT," +
"department TEXT, title TEXT, description TEXT);"
private var _id:int;
private var _name:String;
private var _department:String;
private var _title:String;
private var _description:String;
private var INSERT_PRODUCT:String;
private const SELECT_PRODUCT:String = "SELECT * FROM product";
public function createDatabase(databaseFile:File, productCount:int):void
{
if ((databaseFile != null) && (databaseFile.exists))
{
databaseFile.deleteFile();
dg1.dataProvider=null;
Alert.show("Database Created Successfully.");
}
if (productCount < 1)
{
productCount = 1;
}
mysqlConnection = new SQLConnection();
mysqlConnection.open(databaseFile, SQLMode.CREATE);
mysqlConnection.close();
schemaGeneration();
}
public function deleteDatabase():void
{
if ((databaseFile != null) && (databaseFile.exists))
{
databaseFile.deleteFile();
dg1.dataProvider=null;
Alert.show("Database Deleted Successfully.");
}
else
{
Alert.show("There is no database to Delete.");
}
}
private function schemaGeneration():void
{
var sqlStatement:SQLStatement = new SQLStatement();
mysqlConnection=new SQLConnection();
sqlStatement.sqlConnection = mysqlConnection;
mysqlConnection.open(databaseFile, SQLMode.CREATE);
sqlStatement.text = CREATE_PRODUCT_TABLE;
sqlStatement.execute();
mysqlConnection.close();
}
private function createInsertStatements():void
{
if((txt1.text !="")&&(txt2.text !="")&&(txt3.text !="")
&&(txt4.text !=""))
{
_id=0;
_name=txt1.text;
_department=txt2.text;
_title=txt3.text;
_description=txt4.text;
if((databaseFile != null) && (databaseFile.exists))
{
INSERT_PRODUCT="INSERT INTO product (id, name, department, title, description)" +
" VALUES ("+"((SELECT max(id) FROM product)+1)"+",'"+_name+"','"+_department+
"','"+_title+"','"+_description+"');";
_insertProduct =new SQLStatement();
mysqlConnection=new SQLConnection();
_insertProduct.sqlConnection= mysqlConnection;
mysqlConnection.open(databaseFile, SQLMode.CREATE);
_insertProduct.text = INSERT_PRODUCT;
_insertProduct.execute();
mysqlConnection.close();
createSelectStatements();
clearAll();
}
else
{
Alert.show("There is no database so please create it First.");
}
}
else
{
Alert.show("Please enter the proper values For all the relative fields.");
}
}
private function createSelectStatements():void
{
_selectProduct =new SQLStatement();
mysqlConnection=new SQLConnection();
_selectProduct.sqlConnection = mysqlConnection;
mysqlConnection.open(databaseFile,SQLMode.READ);
_selectProduct.text = SELECT_PRODUCT;
_selectProduct.execute();
ob=_selectProduct.getResult().data;
mysqlConnection.close();
dg1.dataProvider=ob;
}
private function clearAll():void
{
txt1.text="";
txt2.text="";
txt3.text="";
txt4.text="";
}
]]>
</mx:Script>
<mx:VBox width=”100%” height=”100%” horizontalAlign=”center“>
<mx:HBox width=”80%” height=”30” horizontalAlign=”center“>
<mx:Label width=”120” textAlign=”right” text=”Name” color=”#ff0000“/>
<mx:TextInput id=”txt1” width=”120” height=”35“/>
</mx:HBox>
<mx:HBox width=”80%” height=”30” horizontalAlign=”center“>
<mx:Label width=”120” textAlign=”right” text=”Department” color=”#ff0000“/>
<mx:TextInput id=”txt2” width=”120” height=”35“/>
</mx:HBox>
<mx:HBox width=”80%” height=”30” horizontalAlign=”center“>
<mx:Label width=”120” textAlign=”right” text=”Title” color=”#ff0000“/>
<mx:TextInput id=”txt3” width=”120” height=”35“/>
</mx:HBox>
<mx:HBox width=”80%” height=”30” horizontalAlign=”center“>
<mx:Label width=”120” textAlign=”right” text=”Description” color=”#ff0000“/>
<mx:TextInput id=”txt4” width=”120” height=”35“/>
</mx:HBox>
<mx:HBox width=”80%” height=”100%” horizontalAlign=”center“>
<mx:DataGrid id=”dg1” width=”100%” height=”100%“>
<mx:columns>
<mx:DataGridColumn id=”idCol” width=”50” dataField=”id” headerText=”Id” textAlign=”center“/>
<mx:DataGridColumn id=”nameCol” width=”100” dataField=”name”
headerText=”Name” textAlign=”center“/>
<mx:DataGridColumn id=”departmentCol” width=”100” dataField=”department”
headerText=”Department” textAlign=”center“/>
<mx:DataGridColumn id=”titleCol” width=”100” dataField=”title“
headerText=”Title” textAlign=”center“/>
<mx:DataGridColumn id=”descriptionCol” width=”200” dataField=”description“
headerText=”Description” textAlign=”center“/>
</mx:columns>
</mx:DataGrid>
</mx:HBox>
<mx:HBox width=”80%” height=”50” horizontalAlign=”center“>
<mx:Button id=”btnCreateDataBase” label=”Create DataBase” width=”130“
click=”createDatabase(databaseFile,_counterProduct)”/>
<mx:Button id=”btnInsertIntoDataBase” label=”Insert into DataBase” width=”150”
click=”createInsertStatements()”/>
<mx:Button id=”btnDeleteDataBase” label=”Delete DataBase” width=”130”
click=”deleteDatabase()”/>
</mx:HBox>
</mx:VBox>
</mx:WindowedApplication>












