# SQL Database using Adobe Air

How do you create an SQL database (I believe it's tinySQL/SQLite) using Adobe Air? I have searched around, and all sites give code, and then explains to wrap it within an xml document, for example here.

So I have created this xml file, but no one seems to explain what to do from there. I am also VERY new to database creations and management. So any super n00b friendly help would be much appreciated.

Thanks.

Regarding that link you posted: The creation of a SQLite Database has nothing to do with XML, nor is XML needed. What you see there is a MXML document, a special XML flavor introduced by Macromedia/Adobe. But the majority of this document is just plain Actionscript 3 (wrapped in <mx:Script> tags).

So you're going to use Actionscript to create and access the Database. But since you're new to the whole database thing, I suggest you first take a step back and actually learn some SQL. This is going to be easiest by using a tool that allows you to create Databases and run queries. There's even a Firefox addon that allows you to manage SQLite databases from within your Browser.

Once you are comfortable with SQL (you can also look up the exact syntax for SQLite here), the step to using this in Actionscript is really small, because in Actionscript you do nothing else than sending SQL queries (so it's actually good practice to learn the query syntax before you integrate it into more complicated code).

The help documents over in the Adobe Flash help should contain enough information to show you how to use SQLite in Flash/Flex.

Update: Here's a short explanation of the most important SQL commands and when you're typically going to use it in your application.

• When your application starts up, you should check if the Database already exists. If not, you can create one using CREATE TABLE. With CREATE TABLE you specify all the database fields and their types. This can be quite complicated at first, but some of the tools linked above allow you to create the table using a GUI and then "export" the SQL create statements. Example:

CREATE TABLE myTable (
id INTEGER PRIMARY KEY,
name VARCHAR(127)
);

• To read data from one (or multiple) tables, use SELECT. Example:

SELECT * FROM myTable

• To write to a table, use INSERT. Example:

INSERT INTO myTable (id, name) VALUES (1, 'Foo')

• To update data, you use the UPDATE statement. Example:

UPDATE myTable SET name = 'Bar' WHERE id = 1

• To delete data, there's the DELETE statement. Example:

DELETE FROM myTable WHERE id = 1

• Hey thanks for the awesome reply. Sorry if this question is somewhat "dumb" but - I get the whole process of at start up, check if database is created, and if not, create it. And then from there I can issue the SQL commands to retrieve, insert and all that jazz. Do I wrap those start up commands in the class document's constructor (AS3) or is that part of the AIR package? And tbh, I'm not super versed in AIR either, but I know my way around AS3, and have been told that AIR isn't that far off. Thanks again for the very in depth and great answer. :) o7 – Nicholas Apr 26 '12 at 14:19
• It really depends at what point you're going to need the DB. But yes, usually you'll check/create right at the start, better in some init method (listen for CREATION_COMPLETE event) instead of the constructor. Personally I kinda dislike the Actionscript/XML mixture in most of the Flex examples. Usually I strictly separate code (business logic) into AS classes and only keep presentation in MXML files. – bummzack Apr 26 '12 at 14:27