Adobe AIR - local SQL database queries
Below you can find examples of SQL statements(SELECT, INSERT, UPDATE, DELETE).
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; private var sqlConn:SQLConnection; private var sqlFile:File; private var categories:ArrayCollection; private function init():void { sqlFile = File.applicationStorageDirectory.resolvePath("DBSample.db"); sqlConn = new SQLConnection(); sqlConn.open(sqlFile, SQLMode.CREATE); } private function createCategories():void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConn; stmt.text = "CREATE TABLE IF NOT EXISTS categories(" + "categoryid INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT)"; stmt.execute(); var result:SQLResult = stmt.getResult(); categories = new ArrayCollection(result.data); } private function getCategories():void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConn; stmt.text = "SELECT * FROM categories ORDER BY categoryid DESC"; stmt.execute(); var result:SQLResult = stmt.getResult(); } private function addCategory(value:String):void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConn; // Statement stmt.text = "INSERT INTO categories " + "(name) VALUES (:name)"; stmt.parameters[":name"] = value; //OR Statement stmt.text = "INSERT INTO categories " + "(name) VALUES ('"+value+"')"; stmt.execute(); var result:SQLResult = stmt.getResult(); } private function updateCategory(name:String, categoryid:int):void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConn; stmt.text = "UPDATE categories " + "SET name='"+name+"'" + "WHERE " + "categoryid='"+categoryid+"'"; stmt.execute(); var result:SQLResult = stmt.getResult(); getCategories(); } private function removeCategory(categoryid:int):void { var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = sqlConn; // Statement stmt.text = "DELETE FROM categories WHERE categoryid=:categoryid"; stmt.parameters[":categoryid"] = categoryid; // OR Statement stmt.text = "DELETE FROM categories WHERE categoryid='"+categoryid+"'"; stmt.execute(); var result:SQLResult = stmt.getResult(); getCategories(); } ]]> </mx:Script> </mx:WindowedApplication>

I’m Mariusz Tkaczyk.
Treelife:
What happens when you run the application twice. You might get an error since the application will try to run the database creation twice.
What is your solution?
January 5, 2009, 9:30 amMariosh:
The application will run the database creation only once(for non-existing database file).
If you run the application for the first time:
1.The application will run the database creation(creates a database file at that file location).
2.The application will open a connection to the newly created database.
If you run the application for the second time:
1.The application will skip the database creation(database file already exists).
2.The application will open a connection to the database file.
Read more about Creating a database in Adobe’s AIR documentation.
January 5, 2009, 10:34 amTreelife:
Thanks a lot for the info.
I got more info from this page too: http://www.adobe.com/devnet/air/flex/quickstart/simple_sql_database.html
(…)The runtime would then open the database file at that location (creating it first if it doesn’t exist). The code to do that would look like this(…)
January 5, 2009, 2:59 pmMariosh:
I found another great example: SQLite Example for Adobe AIR–Working with local SQL databases(with source code)
January 12, 2009, 9:29 am