<?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/03/air-api-querying-a-local-datab.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.23244-</id>
  <updated>2009-11-16T15:49:10Z</updated>
  <title>Comments for AIR API - Querying a Local Database (http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23244</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.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=23244" title="AIR API - Querying a Local Database" />
    <published>2008-03-31T11:00:50Z</published>
    <updated>2008-03-31T10:58:48Z</updated>
    <title>AIR API - Querying a Local Database</title>
    <summary>

In the previous article, I gave an introduction to SQLite and its place inside or AIR.  In this tutorial, I will be explaining the code needed to connect to and query a previously existing SQLite database.  This will take two articles to accomplish this, so this article will focus on learning the basic classes and methods that you will need to know.  The next tutorial will contain a sample application that implements these features.</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-introduction-to-the-sq.html">previous article</a>, I gave an introduction to SQLite and its place inside or AIR.  In this tutorial, I will be explaining the code needed to connect to and query a previously existing SQLite database.  This will take two articles to accomplish this, so this article will focus on learning the basic classes and methods that you will need to know.  The next tutorial will contain a sample application that implements these features.</p>

<h2><strong>Making the Connection</strong></h2>

<p>The first step in connecting to local database is to create an instance of the SQLConnection class.  Since we are programming asynchronously, we will need to add some event listeners for this class.  The SQLConnection class dispatches many events, but for this example we will be primarily concerned with two: open and  error.  </p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="linecomment">// Instantiate the Class</span>
<span class="category1">var</span> connection:SQLConnection = <span class="category1">new</span> SQLConnection();
<span class="linecomment">// Event Listener that will tell us when the DB is opened</span>
connection.addEventListener(SQLEvent.OPEN, openSuccess);
<span class="linecomment">// Event Listener that will tell us if an error occurs</span>
connection.addEventListener(SQLErrorEvent.ERROR, openFailure);</pre>
</code>

</div></div>

<p>At this point, we haven't actually connected to a database.  If you remember in the last article, I stated that an SQLite database is contained in a single file.  With this being the case, you will first need to create a file object that points to its location.  Since I placed the database in the application storage directory, I will create a reference to it there.</p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="linecomment">// Create a File object that points to the contacts.db file</span>
<span class="category1">var</span> database:File = File.applicationStorageDirectory.resolvePath("<span class="quote">contacts.db</span>");</pre>
</code>

</div></div>

<p>Finally, to actually open a connection to the database, you simply need to call the <code>openAsync</code> method of the SQLConnection instance.  If we were working with the database synchronously, we would use the <code>open</code> method.  We will pass the file object in as the first argument (in either synchronous or asynchronous modes).  If you were to pass <code>null</code> for this first argument, a database would be created in memory (as opposed to a file).</p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="linecomment">// Create a File object that points to the contacts.db file</span>
connection.openAsync( database );</pre>
</code>

</div></div>

<p><em><strong>Note:</strong> If you haven't used the File class before, check out Rich's excepts from his book that have been posted here on InsideRIA.  I have included links at the bottom of this article.</em></p>

<h2><strong>Querying Data</strong></h2>

<p>To actually query the data, you will need to know another class: SQLStatement.  This is the class that will actually contain your SQL Query.  After the class is instantiated, you need to pass the reference to the SQLConnection instance that you created previously for the <code>sqlConnection</code> property.  In most situations, you will want to listen for two different events of this class: result and error.  You can instantiate the class, connect it to SQLConnection, and add its listeners as follows:</p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="linecomment">// Create New Instance</span>
<span class="category1">var</span> query:SQLStatement = <span class="category1">new</span> SQLStatement();
<span class="linecomment">// Add the Connection Reference</span>
query.sqlConnection = connection;
<span class="linecomment">// Add Event Listeners</span>
query.addEventListener( SQLEvent.RESULT, onResult );
query.addEventListener( SQLErrorEvent.ERROR, onError );</pre>
</code>

</div></div>

<p>Since you have created these event listeners, you will also need to create the functions that you just referenced.  We will just create shell methods now, and will implement them in the next tutorial.<p>


<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="category1">private</span> <span class="category1">function</span> onResult( event:SQLEvent ):<span class="category1">void</span> {
 	<span class="linecomment">// The Query Worked</span>
}
<span class="category1">private</span> <span class="category1">function</span> onError( event:SQLErrorEvent ):<span class="category1">void</span> {
 	<span class="linecomment">// There Was an Error</span>
}</pre>
</code>

</div></div>

<p>After these steps, you can create your actual query.  The <code>text</code> property of the SQLStatement class contains the SQL query.  To actually execute the query, you need to call the <code>execute()</code> method of the SQLStatement instance.</p>

<div class="acode" style="overflow:auto;padding: 10px;"><div style="overflow-x:visible;">
<code language="perl">
<pre>
<span class="linecomment">// SQL Query</span>
query.<span class="category2">text</span> = "<span class="quote">SELECT lastName, firstName from contacts</span>";
<span class="linecomment">// Execute the Query</span>
query.execute(); </pre>
</code>

</div></div>

<p>In the next tutorial you will work with an actual AIR application that has a preexisting database.  You will learn how to access the results of an SQLStatement as well as how to determine error information if an error occurs.</p>

<h2><strong>Language Reference</strong></h2>
<p>SQLConnection [ <a href="http://livedocs.adobe.com/flex/3/langref/flash/data/SQLConnection.html" target="_blank">ActionScript</a> , <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/data/SQLConnection.html" target="_blank">JavaScript</a> ]
<br />
SQLStatement [ <a href="http://livedocs.adobe.com/flex/3/langref/flash/data/SQLStatement.html" target="_blank">ActionScript</a>, <a href="http://livedocs.adobe.com/labs/air/1/jslr/flash/data/SQLStatement.html" target="_blank">JavaScript</a> ]
</p>


<h2><strong>Additional Resources</strong></h2>

<p><a href="http://www.insideria.com/2008/03/beginning-air-accessing-the-fi.html" target="_blank">Beginning AIR - Accessing the File System Part 1</a> (Rich Tretola) - InsideRIA<br /><a href="http://www.insideria.com/2008/03/beginning-air-accessing-the-fi-1.html" target="_blank">Beginning AIR - Accessing the File System Part 2 </a>(Rich Tretola) - InsideRIA</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23244-comment:2016443</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23244" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html#comment-2016443" />
    <title>Comment from Matthew Silvera on 2008-04-08</title>
    <author>
        <name>Matthew Silvera</name>
        <uri>http://www.angrysprite.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.angrysprite.com">
        <![CDATA[<p>I have been playing with Air for a while now, and while I love that SQLite was included, but I hate embedding SQL in my AS code. It's 2008, we should not be working this way. For anyone who does work in other languages the lack of an ORM (Object Relational Mapper) is a big one. I have been unable to find anyone who has already built one but have been unable to find one, and that surprises me.</p>

<p>I have started working an all ActionScript ORM for AIR. It is still in the beta stages, but I hope to release a usable version of the code base in the next week or two (by April 21, 2008).</p>

<p>I have started posting code samples to <a href="http://www.angrysprite.com/">http://www.angrysprite.com/</a></p>]]>
    </content>
    <published>2008-04-08T18:02:06Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23244-comment:2016812</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23244" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html#comment-2016812" />
    <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>The SQLLite is wonderful,<br />
but I don't like write sql sentence<br />
one who used hibernate will hate this way<br />
to query data.<br />
If it have a SWC like hibernate will be better.</p>]]>
    </content>
    <published>2008-04-28T13:46:04Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23244-comment:2016813</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23244" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html#comment-2016813" />
    <title>Comment from David Tucker on 2008-04-28</title>
    <author>
        <name>David Tucker</name>
        <uri>http://www.davidtucker.net/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.davidtucker.net/">
        <![CDATA[<p>I know there is a project that is currently looking into this - but, currently that functionality is not available.  It has certainly been brought up many times - so I do expect that something will be developed soon.</p>]]>
    </content>
    <published>2008-04-28T13:48:01Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23244-comment:2045885</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23244" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/air-api-querying-a-local-datab.html#comment-2045885" />
    <title>Comment from Arnaud PEZEL on 2008-11-07</title>
    <author>
        <name>Arnaud PEZEL</name>
        <uri>http://www.matsiya-technology.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.matsiya-technology.com">
        <![CDATA[<p>Hi,</p>

<p>I'm currently working on an sqlite orm for Adobe AIR. It will be released open source soon. You can check about it on my company's website <a href="http://www.matsiya-technology.com">http://www.matsiya-technology.com</a> . There's a quick example.</p>]]>
    </content>
    <published>2008-11-07T12:57:31Z</published>
  </entry>

</feed
