SQL Database Connection with Flex Air

Just create simple air application in Flex.
Using the below code you can able to connect to database.

<?xml version=“1.0″ encoding=“utf-8″?>
<mx:WindowedApplication xmlns:mx=“http://www.adobe.com/2006/mxml”
layout=“absolute” creationComplete=“init()”>
<mx:Script>
<![CDATA[
import mx.controls.Alert;

private var dbConn:SQLConnection;
private var dbFile:File;

private function init():void
{
	dbFile = File.applicationStorageDirectory.resolvePath("flashallys.db");

	dbConn = new SQLConnection();

try
{
	dbConn.open(dbFile, SQLMode.CREATE);
}
catch(e:SQLError)
{
	Alert.show("SQL Error Occured: ", e.message);
}
}
]]>
</mx:Script>
</mx:WindowedApplication>

Here goes the explanation

These two lines are mandatory to declare the SQL connection and to declare db file.

private var dbConn:SQLConnection;
private var dbFile:File;

We apply init funtion for this to dish out the file storage and to establish SQL connection.
private function init():void
{
dbFile = File.applicationStorageDirectory.resolvePath(“flashallys.db”);

dbConn = new SQLConnection();

This part of the code figures out if the output is productive and connects to the database.
try
{
dbConn.open(dbFile, SQLMode.CREATE);
}

Or else,this turns out to display the occurence of an error.
catch(e:SQLError)
{
Alert.show(“SQL Error Occured: “, e.message);
}
}

Tags: , , , , ,

Hi, Stranger! Leave Your Comment...

Name (required)
Email (required)
Website