<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23422-</id>
  <updated>2009-11-16T15:47:00Z</updated>
  <title>Comments for AIR API - Retrieving Results from a Query (http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23422</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23422" title="AIR API - Retrieving Results from a Query" />
    <published>2008-04-17T21:35:33Z</published>
    <updated>2008-04-17T18:36:05Z</updated>
    <title>AIR API - Retrieving Results from a Query</title>
    <summary>

In the previous article, I examined the process for running a simple SELECT query on a pre-populated SQLite database in AIR.  In today&apos;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.</summary>
    <author>
      <name>David Tucker</name>
      <uri>http://www.davidtucker.net/</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r" style="margin: 0 16px 16px 16px;"><a href="http://www.insideria.com/airLogo-Shadow.jpg" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/airLogo-Shadow.jpg" alt="airLogo-Shadow.jpg" title="Click to enlarge" width="148"/></a></div>

<p>In the <a href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html">previous article</a>, 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.</p>

<h2><strong>Retrieving Query Results</strong></h2>

<p>The query results can be retrieved from the instance of the SQLStatement class that was executed.  They can be retrieved by calling the <code>getResult</code> 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:</p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="category1">var</span> r:SQLResult = SQLStatement(event.<span class="category2">target</span>).getResult();</pre>
</code>

</div></div>

<p>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).</p>

<div class="ap_c" style="margin: 16px;"><a href="http://www.insideria.com/dtucker/sqlresult-large.jpg" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/dtucker/sqlresult-large.jpg" alt="sqlresult-large.jpg" title="Click to enlarge" width="400"/></a><div class="apcaption">Figure 1. SQLResult Data</div></div>



<h2><strong>Including a Pre-Populated Database</strong></h2>

<p>If you need to include a database with your AIR application, the process is quite simple.  You will need to follow these steps:</p>

<ol>
<li>Create your SQLite database with one of the tools mentioned in my <a href="http://www.insideria.com/2008/03/air-api-introduction-to-the-sq.html">first article</a> in the series</li>
<li>Place the file in your root source directory (the same directory as your application descriptor file).</li>
<li>The file can now be referenced in the application directory (as shown earlier).
</ol>

<p>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.</p>

<h2><strong>Sample Application</strong></h2>

<p>I have included a sample AIR application which includes a sample SQLite database of fake names and email addresses (generated by <a href="http://www.fakenamegenerator.com/" target="_blank">FakeNameGenerator.com</a>).  This table has 500 rows.</p>

<p>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.</p>

<p>As always, View Source is enabled on this application (and you can also download the source below).</p>

<br />

<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 115;
// AIR Version Required
var airVersion = "1.0";
// AIR Application Name
var airApplicationName = "Basic Query Example";
// AIR Application URL
var airApplicationURL = "http://www.insideria.com/dtucker/sqlite1.air";
// AIR Application Image
var airApplicationImage = "/dtucker/sqlite1.jpg";
// Directory of badge.swf file (optional)
var badgeDirectory = "/dtucker/";
// Directory of playerProductInstall.swf (optional)
var expressInstallDirectory = "/dtucker/"; 
// -----------------------------------------------------------------------------
// -->
</script>

<script src="/dtucker/AC_RunActiveContent.js" language="javascript"></script>
<div style="width: auto;text-align: center">
<script src="/dtucker/InstallBadge.js" language="javascript"></script>
</div>

<br />

<p><strong>Application Source Code</strong><br />
<a href="/dtucker/sqlresult.zip" target="_blank">Download</a> (16.4 kb)</p>

<h2><strong>Reference</strong></h2>

<p>SQLResult [ <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/data/SQLResult.html" target="_blank">ActionScript</a>, <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/data/SQLResult.html" target="_blank">JavaScript</a> ]</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2016814</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2016814" />
    <title>Comment from reallypride on 2008-04-28</title>
    <author>
        <name>reallypride</name>
        <uri>http://www.reallypride.cn</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.reallypride.cn">
        <![CDATA[<p>Read your article,better than read a lot of books</p>]]>
    </content>
    <published>2008-04-28T14:11:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2020388</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2020388" />
    <title>Comment from houstonFlexcoder on 2008-08-06</title>
    <author>
        <name>houstonFlexcoder</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>David,</p>

<p>Now I have followed your instructions directly and I am running into a problem...</p>

<p>import flash.data.SQLResult;<br />
			import flash.data.SQLConnection;<br />
			import flash.filesystem.File;<br />
			import flash.data.SQLStatement;<br />
			import flash.data.SQLConnection;</p>

<p>			private var conn:SQLConnection;<br />
			private var dbFile:File;<br />
			private var createStmt:SQLStatement;<br />
			private var selectStmt:SQLStatement;<br />
			<br />
			<br />
			<br />
			private function init():void<br />
			{<br />
				var dbFile:File = File.applicationStorageDirectory.resolvePath("tagData.sqlite");<br />
				<br />
				conn = new SQLConnection();<br />
				conn.addEventListener(SQLEvent.OPEN, openSuccess);<br />
				conn.addEventListener(SQLErrorEvent.ERROR, openFailure);<br />
				<br />
				status = "Opening database";<br />
	<br />
				conn.openAsync(dbFile);</p>

<p>			}<br />
			<br />
			private function openSuccess(event:SQLEvent):void<br />
			{<br />
				conn.removeEventListener(SQLEvent.OPEN, openSuccess);<br />
				conn.removeEventListener(SQLErrorEvent.ERROR, openFailure);<br />
				</p>

<p>			}<br />
			<br />
			private function openFailure(event:SQLErrorEvent):void<br />
			{<br />
				conn.removeEventListener(SQLEvent.OPEN, openSuccess);<br />
				conn.removeEventListener(SQLErrorEvent.ERROR, openFailure);<br />
				<br />
				status = "Error opening database";<br />
				<br />
				trace("event.error.message:", event.error.message);<br />
				trace("event.error.details:", event.error.details);<br />
            }  </p>

<p>			private function manComboQuery (event:SQLEvent):void <br />
			{<br />
				var q:SQLStatement = new SQLStatement();<br />
				q.text = "Select ManufacturerName FROM Manufacturers"<br />
				q.sqlConnection = conn;<br />
				q.addEventListener(SQLEvent.RESULT, manQueryResult);<br />
				q.addEventListener(SQLErrorEvent.ERROR, manQueryError);<br />
				q.execute();<br />
			}<br />
			<br />
			private function manQueryResult (event:SQLEvent):void<br />
			{<br />
				var r:SQLResult = SQLStatement(event.currentTarget).getResult();<br />
				mandp.dataprovider = r.data;<br />
			}<br />
			<br />
			private function manQueryError (event:SQLErrorEvent):void<br />
			{<br />
				trace("BREAKER");<br />
			}</p>

<p>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</p>

<p>error is as follows: <br />
1119: Access of possibly undefined property dataprovider through a reference with static type mx.controls:ComboBox.</p>

<p>when setting the id to mandp</p>

<p>when setting the dataprovider to mandp error is as follows:<br />
1120: Access of undefined property mandp.	</p>

<p>Please bear in mind that I'm a noob at all of this and I am confused as helll... HELP!!!<br />
</p>]]>
    </content>
    <published>2008-08-06T16:54:21Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2143849</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2143849" />
    <title>Comment from Tim on 2009-10-17</title>
    <author>
        <name>Tim</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>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. </p>

<p>** 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.**</p>

<p>select t1.id, t1.descrip, t2.title<br />
from t1 inner join t2 on t1.id=t2.id<br />
where t1.descrip = 'xxx'</p>

<p>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.</p>

<p>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:</p>

<p>t1.id  = 140 and the PK of title returned is 1400.<br />
t1.id = 160 and the PK of the title returned is 1610</p>]]>
    </content>
    <published>2009-10-17T13:32:26Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2143850</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2143850" />
    <title>Comment from Tim on 2009-10-17</title>
    <author>
        <name>Tim</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>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. </p>

<p>** 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.**</p>

<p>select t1.id, t1.descrip, t2.title<br />
from t1 inner join t2 on t1.id=t2.id<br />
where t1.descrip = 'xxx'</p>

<p>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.</p>

<p>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:</p>

<p>t1.id  = 140 and the PK of title returned is 1400.<br />
t1.id = 160 and the PK of the title returned is 1610</p>]]>
    </content>
    <published>2009-10-17T13:33:40Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2143859</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2143859" />
    <title>Comment from Tim on 2009-10-17</title>
    <author>
        <name>Tim</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>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.</p>]]>
    </content>
    <published>2009-10-17T13:59:59Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23422-comment:2149705</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23422" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/air-api-retrieving-results-fro.html#comment-2149705" />
    <title>Comment from Ahmed on 2009-10-21</title>
    <author>
        <name>Ahmed</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi David</p>

<p>How to use the result of query ( select statement) in Charts?</p>

<p>Ahmed</p>]]>
    </content>
    <published>2009-10-22T05:02:29Z</published>
  </entry>

</feed
