Sunday, May 1, 2011

Working with SQLite in AIR: Part 8

Today we will learn how to execute SQL commands using AS3 in AIR.

In the previous tutorials we have covered several SQL commands, such as CREATE TABLE, INSERT, DELETE, SELECT, UPDATE and more. To execute them through AIR, we need to use the SQLStatement class.

Firstly we need to create a SQLStatement object. It is done like this:

var myStat:SQLStatement = new SQLStatement();

When you are tring to execute a command, you need to tell the SQLStatement object which SQLConnection object is being used for the database you want to work with. It can be done by applying the SQLConnection object to sqlConnection property of the statement:

myStat.sqlConnection = myConnection;

Now you need to enter the SQL command you want to execute. This is done through the text property of the statement object. In the next example I'm passing a SELECT command:

myStat.text = "SELECT fname FROM contacts";

Now that we have set up the command, we still have to execute it using the execute() method. This can be done synchronously and asynchronously, and it depends on whether you've set your connection as synchronous or asynchronous.

If you are connecting to the databse synchronously, execute() will work and get the results immediately, but if you're doing it asynchronously - the results won't be there immediately. The results of the operation will throw one of the two possible events - result or error.

If we call the execute() method without specifying any parameters, we need to set event listeners for these two events directly for the SQLStatement. For example, to catch the result event, we would have this event listener:

myStat.addEventListener(SQLEvent.RESULT, onResult);

To catch an error event:

myStat.addEventListener(SQLEvent.ERROR, onError);

However, we can also handle the results of the execution using a Responder object. If we do that, we won't need to manually listen for the result and error events. All we have to do is pass the Responder object as a parameter in the execute() method.

The first parameter of the method is responsible for outputting the data. We'll learn about that in detail later, so lets leave it as default for now, and that value is -1 (which means output all data). Responder comes as second parameter, where we pass the functions that are called on result and error events:

myStat.execute(-1, new Responder(onResult, onError));

Where onResult and onError are the functions used for processing the data. We'll learn about that in the next tutorial.

Thanks for reading!

Related:

Working with SQLite in AIR: Part 1
Working with SQLite in AIR: Part 2
Working with SQLite in AIR: Part 3
Working with SQLite in AIR: Part 4
Working with SQLite in AIR: Part 5
Working with SQLite in AIR: Part 6
Working with SQLite in AIR: Part 7
Working with SQLite in AIR: Part 9
Working with SQLite in AIR: Part 10
Working with SQLite in AIR: Part 11
Working with SQLite in AIR: Part 12

No comments:

Post a Comment