SQLite is a relational database right inside Adobe AIR environment. While its a full fledged relational database system,the real power of it comes from the ease of use. There is no server to configure,no server to start. Once Adobe Integrated Runtime (AIR) is installed in your system, you are ready to take advantage of SQLite database. Actually SQLite comes with the AIR itself. So there is absolute zero configuration as it is clearly noted in the SQLite official site.
Now,all that one need to know is the Flash (Actionscript 3.0) APIs to start talking with the Database.
To begin with any database applications,one need to have a database in a database server,start the server,connect to it and then start putting in data or getting the data. Now in this case of SQLite, we already have the initial set up ready for us. I mean as a SQLite developer on Flash platform one need not worry about the server setup and starting the Database server. All one need to concentrate is, connecting to the Database and puting some data in or getting some data out from the database. For once AIR is installed SQLite is up and running,how cool 🙂 ! Now the best part is,all the standard SQL query runs in the SQLite Database too. To get started, one need to create a database or connect to it,if already created. Now SQLite API gives us one syntax to do the both,I mean if the database is already present it just connects to it else it creates the Database for us and then connects to it. The syntax is as below
var conn = new SQLConnection();//Connect to the DB server
//adds event listeners to listen for the events
var conn.addEventListener(SQLEvent.OPEN, onOpenConnection);
var conn.addEventListener(SQLErrorEvent.ERROR, onConnectionError);
//connects or creates and connects to the database
var dbFile:File = File.applicationDirectory.resolvePath(“myDb.db”);
conn.openAsync(dbFile);//actually connecting to the Database
Thats all to create and connect to the database. By convention the database files are named with extention “db”,In this example we are creating a Database file named “myDb.db”. The Database is actually stored as a file,as we created now. To make any query to the Database the syntax will go as below
var sqlStatement:SQLStatement = new SQLStatement();
sqlStatement.sqlConnection = conn;//”conn” is the connectino we just made with the above code
var query:String = “SELECT * FROM friends”;//This is actual SQL query we are going to run in the Database
sqlStatement.text = query;
//adds event listeners to see if our query ran properly
sqlStatement.execute();//finally calls to run the query in the Database
Thats all to it. 🙂
Now to make things easier for my project,I have created a helper class for regular tasks as querying the Database and getting the result. Please find the file at google code repository here. Hope that helps you on your day to day task of SQLite development under AIR and Flash platform.