开发者

Querying SQLite database with Flex/AIR app

开发者 https://www.devze.com 2023-01-30 14:03 出处:网络
I have been struggling with trying to interface with SQLite and Adobe AIR.I am using the Flex 4 SDK with AIR 2.0.I modified code from a tutorial by David Tucker on InsideRIA (http://insideria.com/2008

I have been struggling with trying to interface with SQLite and Adobe AIR. I am using the Flex 4 SDK with AIR 2.0. I modified code from a tutorial by David Tucker on InsideRIA (http://insideria.com/2008/04/air-api-additional-query-tech.html). I am writing in FlashDevelop (which I suppose could be my problem), and I can compile and run the app. The issue is that when I go to search the db file, I always get some error which results in the 'BREAKER' trace (ie - the 'queryError' function is called). I have tried simply running the SQL command on the db and it works fine, so there is an issue somewhere in the way Flex or AIR is handling my query (or obviously I could have made a mistake in coding). Thank you to whoever may be able to help. My code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
  xmlns:TestSQL="*"
  xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  xmlns:fl="http://code.google.com/p/flexlib/"
  xmlns:views="com.views.*"
  layout="absolute" 
  showFlexChrome="false"
  showStatusBar="false"
  creationComplete="init()">
  <!-- xmlns:com="components.*" -->

  <fx:Script>
  <![CDATA[
        import flash.data.SQLStatement;
        import flash.errors.SQLError;
        import flash.events.Event;
        import flash.events.SQLErrorEvent;
        import flash.events.SQLEvent;
        import flash.events.TimerEvent;
        import flash.filesystem.File;
        import flash.utils.Timer;

        // -- PROPERTIES ------------------------------------------------------------- /

        private var dbFile:File;
        private var conn:SQLConnection;

        // -- AUTO INIT FUNCTIONS --------------------------------------------------- /

        private function init():void {

            // Create a File Reference to the Included DB
            dbFile = File.applicationDirectory.resolvePath( "contacts.db" );

            // Create SQL Connection
            conn = new SQLConnection();
            conn.openAsync( dbFile );

        }

        private function callQuery():void {
            var q:SQLStatement = new SQLStatement();
            q.sqlConnection = conn;
            q.addEventListener( SQLEvent.RESULT, queryResult );
            q.开发者_开发技巧addEventListener( SQLErrorEvent.ERROR, queryError );

            var sql:String = "SELECT DISTINCT contacts.lastName, contacts.firstName, contacts.email " +
                    "FROM contacts " +
                    "WHERE lastName LIKE :searchString " + 
                    "OR firstName LIKE :searchString";

            q.parameters[":searchString"] = "%" + searchString.text + "%";
            q.text = sql;
            q.execute();
        }

        private function queryResult( event:SQLEvent ):void {
            var r:SQLResult = SQLStatement(event.currentTarget).getResult();
            dg.dataProvider = r.data;
        }

        private function queryError( event:SQLErrorEvent ):void {
            trace("BREAKER");
        }

]]>
</fx:Script>

<mx:VBox>
    <mx:TextInput id="searchString" change="callQuery()" enabled="true" />
    <mx:DataGrid id="dg" width="100%" height="100%">
        <mx:columns>
            <mx:DataGridColumn id="lastName" dataField="lastName" headerText="Last Name" />
            <mx:DataGridColumn id="firstName" dataField="firstName" headerText="First Name" />
            <mx:DataGridColumn id="email" dataField="email" headerText="Email Address" />
        </mx:columns>
    </mx:DataGrid>
    </mx:VBox>
 </mx:WindowedApplication>


Are you sure the database is being opened? To make debugging easier (since by default many errors are silent), try the following.

_responder = new Responder(resultEventHandler, errorEventHandler);
conn = new SQLConnection();
conn.openAsync(dbFile, SQLMode.READ, _responder);           

where the two event handlers indicate if the open is okay. Also, change your result error handler to

    private function queryError( event:SQLErrorEvent ):void {
        trace("ERROR: ", event.error );
    }

So you can see the full error, instead of just BREAKER.

0

精彩评论

暂无评论...
验证码 换一张
取 消