Adobe AIR and Flex - SQLConnection

Skill

Adobe AIR and Flex - SQLConnection

Posted in:

Adobe added the ability to use local databases when they created AIR. This is one of the many features that help make Adobe AIR a great solution for cross platform desktop applications. In this quick tutorial I am going to show how to create a database file and open a connection to it. The code is very simple so let's jump right into it.

The first thing is setting up a file for the database. In SQLite, which is the database engine used in Adobe AIR, the entire database is just stored in a single file. It is actually a really nice compact and portable system. I used the applicationStorageDirectory function on the File class to create a File that points to the local application storage and is named tut.db. In my case (in Vista) the actual path on disk resolves to C:\Users\User\AppData\Roaming\SQLConnectionTutorial\Local Store. The file isn't actually created until we make another call.

<?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("tut.db");

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

Above it the entire code for creating the database file and opening the connection. We use the open function on the SQLConnection class to open the connection. It is also advised to wrap any database functions in a try/catch block in order to catch any sql errors that may occur.

Once you have connected to the database you can do many things. Most of the operations are done using the SQLStatement class, which allows you to query the database for typical retrieving, inserting, updating, and deleting calls. One of very first things, though, is creating database table(s) and adding data. This is obviously needed before you can actually store/retrieve any data. The local database also has many other nuances that I will try to cover in further detail later on.

This is just quick intro to the local database support in Adobe AIR. Look forward to some more tutorials on the different aspects of the local databases. If you have any questions feel free to leave a comment.

Ofis Mobilyaları
06/14/2008 - 02:05

thanks.

reply

Khairunnisa
07/24/2008 - 23:47

"The file isn't actually created until we make another call."

What if I already have an sqlite database file, with data in it.. I don't want to create but just call it.. How do I call it in Vista?

Can I still use File.applicationStorageDirectory?

reply

The Fattest
07/25/2008 - 08:11

Khairunnisa, that should work just fine. all the file directory calls are platform agnostic.

reply

dieyana
02/19/2009 - 20:34

hye...this is great!!i have been searching about SQLite and Adobe AIR but nothing fulfill my needs.thank you so much to you.but could you write more about this??on how to create database,insert the data,query and retrieve data from the SQLite for Flex application.what u have posted is on Php.that would be great.

thanks in advance..

reply

dieyana
02/20/2009 - 04:13

ok..i have created my database.so you can ignore my first reply.=)

my problem now is,i have created one database on my desktop using one SQLite administration tool that i have downloaded.can you show me how to connect my database with my application that i have build using flex(AIR Application).i don't ave any idea how to do it

reply

The Fattest
02/20/2009 - 09:49

It would be exactly like the above except now you need to point to the database file on your desktop so something like.

dbFile = File.desktopDirectory.resolvePath("filename");

dbConn = new SQLConnection();
     
try
{
  dbConn.open(dbFile, SQLMode.UPDATE);
}
catch(e:SQLError)
{
  Alert.show("SQL Error Occured: ", e.message);
}

reply

dieyana
02/22/2009 - 20:46

hye...

i have some problem with mySQLite database.i have this code for my application.when you clik the load button,it will load a text file on the desktop directory.when you click the filter function it will split the text file according to an array that i have specified.
the text file consist of some data like this:66.249.71.54 - - [10/Oct/2008:03:31:01 +0800] "GET /mod.php?_call=Kalendar&day=2014-05-09 HTTP/1.1" 200
66.249.71.54 - - [10/Oct/2008:03:31:57 +0800] "GET /KERIAN%20ENG/Towns/Main_Town.htm HTTP/1.0" 404
i have created my database on my desktop using one tool that i have downloaded and point to that database.-the code here.
the problem now is,i want to store the "200" only strings into the database.strings other than "200" will be not stored in the database.can u show me how to do this..

i hope you can understand what i want...

thanks for your time..

//code for my application//
<?xml version="1.0" encoding="utf-8"?>

//code for mySQL//
<?xml version="1.0" encoding="utf-8"?>

reply

1050165
03/05/2009 - 03:24

good tutorial!!i want to ask what if i want to save my data to the database.what should i do?my application have some data in it,and a button "save" to save the data in the database.can you show me how to do this?so that i can learn...

thanks in advance!!

reply

slytherin
03/12/2009 - 22:55

hye...i'm interested in you article.but u didn't mention on how an application can save data in the database.for example,when user click the 'save' button,all the data in that application can be saved into the database.

reply

Benarius
05/28/2009 - 21:46

same problem here...I am still new to Flex. I am able to design a homepage with html php and mysql data ,etc. In Flex there is always talk about XML. I worked out how to use XML files with data grid, etc. I just don't understand how can I get my data from my database into the XML format. Please help. Thanks.

reply

Anonymous
08/20/2009 - 10:13

To get data from database in XML format, you parse it with PHP, then send it to Flex as XML.

PHP gets the data from the database as Array, right? for each element in that array you have to create the structure as XML so when you send it to Flex, this one can intepret it as is (XML)

Find out more about this at php.net/simplexml

reply

sumit
10/30/2009 - 10:25

Any one know how we use sqlconnection in Flex 3.0 with asp.net
please send a example to me at sumit.rahul@hotmail.com

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors