简体   繁体   English

使用Flex / AIR应用程序查询SQLite数据库

[英]Querying SQLite database with Flex/AIR app

I have been struggling with trying to interface with SQLite and Adobe AIR. 我一直在努力尝试与SQLite和Adobe AIR交互。 I am using the Flex 4 SDK with AIR 2.0. 我将Flex 4 SDK与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 ). 我修改了David Tucker在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. 我正在用FlashDevelop编写(我想这可能是我的问题),并且可以编译和运行该应用程序。 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). 问题是,当我搜索数据库文件时,总是会遇到一些错误,从而导致“ BREAKER”跟踪(即-调用了“ queryError”函数)。 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). 我尝试在数据库上简单地运行SQL命令,它运行良好,因此Flex或AIR处理查询的方式存在问题(或者显然我在编码中可能犯了一个错误)。 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. 因此,您可以看到完整的错误,而不仅仅是BREAKER。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM