SQLlite Database

AIR comes with an embedded version of SQLLite database, and I realize that many developers moving to AIR will come from a traditional web developement background and will be accustomed to interacting with a database using SQL through other server side language as PHP, .NET, JAVA …  The inclusion of SQLLite in AIR allows developers to use their current SQL skills while interacting with a local offline database specific to their AIR application.

I will not focus on SQLLite, assuming you have already been familiar with it. This example just gives you the way to access to your own SQLLite with AIR application written in Adobe Flex. The subclassing will be used to illustrate

MainApplication.as 

package com.app {
	import flash.data.SQLConnection;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;

	import mx.controls.Alert;
	import mx.core.WindowedApplication;
	import mx.events.FlexEvent;

	public class MainApplication extends WindowedApplication {
		private var conn: SQLConnection;

		public function MainApplication() {
			super();
			this.configureEventListeners();
			this.setupDatabase();
		}

		private function configureEventListeners(): void {
			this.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleleHandler);
		}

		private function setupDatabase():void {
			conn = new SQLConnection();
			conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			var dbFile: File = File.applicationStorageDirectory.resolvePath("d:/temp/MyDB.db");
			if(dbFile.exists) {
				conn.addEventListener(SQLEvent.OPEN, openDatabaseHandler);
			}
			else {
				conn.addEventListener(SQLEvent.OPEN, createDatabaseHandler);
			}
			conn.openAsync(dbFile);
		}

		private function errorHandler(event: SQLErrorEvent):void {
			var err:String = "Error id:" + event.error.errorID + "\nDetails:" + event.error.details;
			Alert.show(err, "Error");
		}

		private function createDatabaseHandler(event: SQLEvent):void {
			Alert.show("Create Database !");
		}

		private function openDatabaseHandler(event:SQLEvent):void {
			Alert.show("Open Database !");
		}

		protected function applicationCompleleHandler(event: FlexEvent): void {

		}
	}
}
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<app:MainApplication
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:app = "com.app.*"
    layout="absolute">
</app:MainApplication>

Giving you a trick to work with SQLLite database. You should intall  SQLLite Manager add-on for Mozilla FireFox to manage your database.

SQLLite Manager add-on
SQLLite Manager add-on

2 thoughts on “SQLlite Database

  1. Chao anh Lam, em la 1 trong so cac member Flash o Pyco vao sau khi anh nghi. Tinh co phat hien duoc blog anh rat la ly thu hi vong se duoc anh chia se va hoc hoi them nhieu kien thuc tu anh. Thank anh vi cac bai viet😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s