Home >
In the previous article, I examined the process for running a simple SELECT query on a pre-populated SQLite database in AIR. In today's example, I will actually execute a simple SELECT query and use the results to populate a data grid. I will also look at how to include a pre-populated database with an AIR application.
Retrieving Query Results
The query results can be retrieved from the instance of the SQLStatement class that was executed. They can be retrieved by calling the getResult of this instance. This will return an Array of results. The SQLEvent that is dispatched when the query is complete will contain a reference to the SQLStatement that fired it. You can get the results with this code:
var r:SQLResult = SQLStatement(event.target).getResult();
As you can see from the code, the value that is returned is of type SQLResult (link to documentation below). The data property of this class provides an Array of objects for your query results. This can be seen in the image below (which shows what the data property looks like inside of Flex Builder's variables view).
Including a Pre-Populated Database
If you need to include a database with your AIR application, the process is quite simple. You will need to follow these steps:
- Create your SQLite database with one of the tools mentioned in my first article in the series
- Place the file in your root source directory (the same directory as your application descriptor file).
- The file can now be referenced in the application directory (as shown earlier).
It would be a good idea to move the file to the application storage directory for each user (this directory is unique per user). To do this, you would simply need to check on launch to see if the file existed. If not, you could simply copy it over to the application storage directory. This is ideal especially if this data will be edited by the end user.
Sample Application
I have included a sample AIR application which includes a sample SQLite database of fake names and email addresses (generated by FakeNameGenerator.com). This table has 500 rows.
When the application launches, you will see a connect button enabled. When you click this button, it will connect to the database, and you can then click the query button. This will execute the sample query and populate the data grid below with the data from the query.
As always, View Source is enabled on this application (and you can also download the source below).
Application Source Code
Download (16.4 kb)
Reference
SQLResult [ ActionScript, JavaScript ]






Facebook Application Development
Read your article,better than read a lot of books
David,
Now I have followed your instructions directly and I am running into a problem...
import flash.data.SQLResult;
import flash.data.SQLConnection;
import flash.filesystem.File;
import flash.data.SQLStatement;
import flash.data.SQLConnection;
private var conn:SQLConnection;
private var dbFile:File;
private var createStmt:SQLStatement;
private var selectStmt:SQLStatement;
private function init():void
{
var dbFile:File = File.applicationStorageDirectory.resolvePath("tagData.sqlite");
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openSuccess);
conn.addEventListener(SQLErrorEvent.ERROR, openFailure);
status = "Opening database";
conn.openAsync(dbFile);
}
private function openSuccess(event:SQLEvent):void
{
conn.removeEventListener(SQLEvent.OPEN, openSuccess);
conn.removeEventListener(SQLErrorEvent.ERROR, openFailure);
}
private function openFailure(event:SQLErrorEvent):void
{
conn.removeEventListener(SQLEvent.OPEN, openSuccess);
conn.removeEventListener(SQLErrorEvent.ERROR, openFailure);
status = "Error opening database";
trace("event.error.message:", event.error.message);
trace("event.error.details:", event.error.details);
}
private function manComboQuery (event:SQLEvent):void
{
var q:SQLStatement = new SQLStatement();
q.text = "Select ManufacturerName FROM Manufacturers"
q.sqlConnection = conn;
q.addEventListener(SQLEvent.RESULT, manQueryResult);
q.addEventListener(SQLErrorEvent.ERROR, manQueryError);
q.execute();
}
private function manQueryResult (event:SQLEvent):void
{
var r:SQLResult = SQLStatement(event.currentTarget).getResult();
mandp.dataprovider = r.data;
}
private function manQueryError (event:SQLErrorEvent):void
{
trace("BREAKER");
}
is the code that I'm using for my manufacturer combobox... the id of my combobox in mandp - tried setting the dataprovider as mandp and Flex fired off an error - set the id to mandp and still have an error
error is as follows:
1119: Access of possibly undefined property dataprovider through a reference with static type mx.controls:ComboBox.
when setting the id to mandp
when setting the dataprovider to mandp error is as follows:
1120: Access of undefined property mandp.
Please bear in mind that I'm a noob at all of this and I am confused as helll... HELP!!!
Are you able to get selects working in Flex against SQlite when they involve inner joins on a production database, i.e. one where you might find "adjacent" keys that would sort differently if sorted alpha versus sorted numerically, e.g. 140, 1400, 160, 1610, and so forth? I cannot. The wrong joined values are being returned in Flex, but only in Flex and in GUI tools based on the Flex data libraries.
** But If I run the identical queries in other GUI tools against the same SQLite database (eg. Razor, SQLMaestro, Visual Studio) the correct results are returned.**
select t1.id, t1.descrip, t2.title
from t1 inner join t2 on t1.id=t2.id
where t1.descrip = 'xxx'
The rows returned correctly satisfy the where condition in the sense that no row returned fails to meet the condition, but the t2.title values are always wrong.
When I look into the problem, examining the PK of the TITLE rows that are returned by the join, those PKs are NEARBY BUT DO NOT EQUAL t1.id. For example:
t1.id = 140 and the PK of title returned is 1400.
t1.id = 160 and the PK of the title returned is 1610
Are you able to get selects working in Flex against SQlite when they involve inner joins on a production database, i.e. one where you might find "adjacent" keys that would sort differently if sorted alpha versus sorted numerically, e.g. 140, 1400, 160, 1610, and so forth? I cannot. The wrong joined values are being returned in Flex, but only in Flex and in GUI tools based on the Flex data libraries.
** But If I run the identical queries in other GUI tools against the same SQLite database (eg. Razor, SQLMaestro, Visual Studio) the correct results are returned.**
select t1.id, t1.descrip, t2.title
from t1 inner join t2 on t1.id=t2.id
where t1.descrip = 'xxx'
The rows returned correctly satisfy the where condition in the sense that no row returned fails to meet the condition, but the t2.title values are always wrong.
When I look into the problem, examining the PK of the TITLE rows that are returned by the join, those PKs are NEARBY BUT DO NOT EQUAL t1.id. For example:
t1.id = 140 and the PK of title returned is 1400.
t1.id = 160 and the PK of the title returned is 1610
I've been able to zero in on the problem a bit. The joined table is giving Flex problems. I can get wrong values back in a simple select against that table alone. But no other GUI tool is having a problem with it.
Hi David
How to use the result of query ( select statement) in Charts?
Ahmed