Flashscript classes (ActionScript 3) | Home | MX | MX 2004 | Flash 8 | Flash 9 |Flash 10 |PHP |Components | Snippets | Contact |

SQLDatabase (Adobe AIR only)

Package:
Class:
Inheritance:
biz.flashscript.adobeair
public class SQLDatabase
EventDispatcher
The SQLDatabase class processes a local Database for Adobe Air. The Database can be moved to different locations and placed in folders. Using this class a table can be created for the Database. Use the constructor new SQLDatabase().
Public Properties
Property Defined By
myURL : String
To add the name of the Database.
SQLDatabase
directory : String
To determine the location of the Database.
SQLDatabase
sqlTable : String
The table contents.
SQLDatabase
sqlResult : SQLResult
To get the table contents.
SQLDatabase
Public Methods
Method Defined By
SQLDatabase()
Creates a new instance of the SQLDatabase class.
SQLDatabase
makeSQLConnection(url:String, folder:String=null, folderTwo:String=null)
Creates a new Database.
SQLDatabase
setTableParams(sql:String)
Creates the table.
SQLDatabase
processTable(sqlData:String)
Function to add data to the table.
SQLDatabase
Events
Event Defined By

tableCreated

This event is dispatched when the table is created.

SQLDatabase

dataProcessed

This event is dispatched when the data are successfully added to the table.

SQLDatabase
All inherited events for the EventDispatcher class. N/A
Property Detail
myURL property
myURL:String [read-write]

This adds the name of the Database.

Implementation

public function get myURL():String
public function set myURL(value:String):void
directory property
directory:String [read-write]

This will determine the location of the database. The locations are "storage", "user", "desktop" and "documents". The default location is "storage" (applicationStorageDirectory).

Implementation

public function get directory():String
public function set directory(value:String):void
sqlTable property
sqlTable:String [read-write]

This will create a new table.

Implementation

public function get sqlTable():String
public function set sqlTable(value:String):void
sqlResult property
sqlResult:SQLResult [read]

This function allows to get the table contents.

Implementation

public function get sqlResult():SQLResult
public function set sqlResult(value:SQLResult):void
Constructor Detail
SQLDatabase ()  Constructor
public function SQLDatabase()
Intializes a new SQLDatabase instance.
Method Detail

makeSQLConnection()  method
public function makeSQLConnection(url:String, folder:String=null, folderTwo:String=null):void

This method will initiate the database creation.

Parameters

  • url:String ____ Specifies the name for the database.
  • folder:String ____ Optional: the name of the first folder to place the database. Default is null.
  • folderTwo:String ____ Optional: the name of a second folder to place the database. Default is null.

setTableParams()  method
public function setTableParams(sql:String):void

Function will create a table.

Parameters

  • sql:String ____ Specifies the table parameters.

processTable()  method
public function processTable(sqlData:String):void

Function allows adding data to the table using SQL syntax.

Parameters

  • sqlData:String ____ data for the table.
Event Detail

tableCreated event

Event Object Type: biz.flashscript.adobeair.SQLDatabase
Event.type property = biz.flashscript.adobeair.SQLDatabase.TABLE_CREATED

Dispatched when the table is created.

dataProcessed event

Event Object Type: biz.flashscript.adobeair.SQLDatabase
Event.type property = biz.flashscript.adobeair.SQLDatabase.DATA_PROCESSED

Dispatched when the data are successfully added to the table.

Examples
Create a new fla file and name it SQLDatabase.fla. Set the Publish settings to Adobe Air. Place the fla in the same folder as the biz folder. Put a DataGrid component (myGrid), three TextInput components (nameField, salaryField, ageField) and two Button components (submitBut, searchBut) on the stage. Then create an Actionscript file, name it SQLDatabaseExample.as and place this script.
package
{
	import flash.display.Sprite;
	import biz.flashscript.adobeAir.SQLDatabase;
	import flash.events.*;
	import flash.data.SQLConnection;
	import fl.controls.Button;
	import fl.controls.TextInput;
	import flash.data.SQLResult;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;

	public class SQLDatabaseExample extends Sprite
	{
		public var submitBut:Button;
		public var searchBut:Button;
		public var nameField:TextInput;
		public var salaryField:TextInput;
		public var ageField:TextInput;
		public var myGrid:DataGrid;
		private var conn:SQLConnection;
		private var mySQL:SQLDatabase;
		private static var tableIsok:Boolean = false;

		public function SQLDatabaseExample ():void
		{
			mySQL = new SQLDatabase();
			mySQL.directory = "desktop";
			mySQL.makeSQLConnection ("Test.DB", "SQLTest", "Local Store");
			var sql:String = "";
			sql +=  "CREATE TABLE IF NOT EXISTS employee (";
			sql +=  "Id        	INTEGER PRIMARY KEY AUTOINCREMENT, ";
			sql +=  "user		TEXT, ";
			sql +=  "salary		TEXT, ";
			sql +=  "age		TEXT";
			sql +=  ")";
			mySQL.sqlTable = sql;
			mySQL.addEventListener ("tableCreated", resultFunction);
			submitBut.label = "Add data";
			submitBut.addEventListener (MouseEvent.CLICK, insertData);
			searchBut.label = "Search";
			searchBut.addEventListener (MouseEvent.CLICK, searchData);
			/*
			/ We define the columns for the DataGrid.
			*/
			var no:DataGridColumn=new DataGridColumn();
			var item:DataGridColumn=new DataGridColumn();
			var price:DataGridColumn=new DataGridColumn();
			myGrid.columns = ["name", "salary", "age"];
			myGrid.getColumnAt(0).width = 175;
			myGrid.getColumnAt(1).width = 75;
			myGrid.getColumnAt(2).width = 50;
			myGrid.getColumnAt(0).headerText = "NAME";
			myGrid.getColumnAt(1).headerText = "Salary";
			myGrid.getColumnAt(2).headerText = "Age";
		}
		private function insertData (event:Event):void
		{
			if (tableIsok && nameField.text != "" && salaryField.text != "" && ageField.text != "")
			{
				var sql:String = "";
				sql +=  "INSERT INTO employee (user, salary, age) ";
				sql +=  "VALUES ('" + nameField.text + "', '" + salaryField.text + "', '" + ageField.text + "')";
				mySQL.processTable (sql);
				mySQL.addEventListener ("dataProcessed", resultFunction);
			}
			else
			{
				trace ("Fill out all fields");
			}
		}
		private function searchData (event:Event):void
		{
			var sql:String = "";
			sql +=  "SELECT * FROM employee";
			mySQL.processTable (sql);
			mySQL.addEventListener ("dataProcessed", searchFunction);
		}
		private function searchFunction (event:Event):void
		{
			myGrid.removeAll();
			event.currentTarget.removeEventListener ("dataProcessed", searchFunction);
			var result:SQLResult = mySQL.sqlResult as SQLResult;
			if (result.data != null)
			{
				var numRows:int = result.data.length;
				for (var i:int = 0; i < numRows; i++)
				{
					var row:Object = result.data[i];
					if (nameField.text != "" && salaryField.text == "" && ageField.text == "")
					{
						if (String(nameField.text).search(row.user) != -1)
						{
							if (row.user != null)
							{
								myGrid.addItem ({name:row.user, salary:row.salary, age:row.age});
							}
						}
					}
					if (nameField.text == "" && salaryField.text != "" && ageField.text == "")
					{
						if (String(salaryField.text).search(row.salary) != -1)
						{
							if (row.user != null)
							{
								myGrid.addItem ({name:row.user, salary:row.salary, age:row.age});
							}
						}
					}
					if (nameField.text == "" && salaryField.text == "" && ageField.text != "")
					{
						if (String(ageField.text).search(row.age) != -1)
						{
							if (row.user != null)
							{
								myGrid.addItem ({name:row.user, salary:row.salary, age:row.age});
							}
						}
					}
				}
			}
		}
		private function resultFunction (event:Event):void
		{
			event.currentTarget.removeEventListener ("dataProcessed", resultFunction);
			if (event.type == "tableCreated")
			{
				trace ("Table created");
				tableIsok = true;
			}
			if (event.type == "dataProcessed")
			{
				trace ("Data processed");
			}
		}
	}
}