1

我正在尝试检索 flex 数据库(AIR APPLICATION)中的数据,下面是代码。一切正常,除了我无法检索数据。即,当执行选择语句时,执行后不调用结果处理程序

   package
   {
import flash.data.SQLConnection;
import flash.data.SQLMode;
import flash.data.SQLResult;
import flash.data.SQLSchemaResult;
import flash.data.SQLStatement;
import flash.data.SQLTableSchema;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.events.TimerEvent;
import flash.filesystem.File;
import flash.utils.Timer;

import mx.core.FlexGlobals;

public class DB
{
    public var conn:SQLConnection;
    private var myTimer:Timer
    private var selectStmt:SQLStatement;
    private var insertStmt:SQLStatement;
    public function DB()
    {           
        conn = new SQLConnection();

        var dbFile:File = File.applicationStorageDirectory.resolvePath("DBSample1.db");

        try
        {

            conn.openAsync(dbFile);
            myTimer = new Timer(1000, 1);
            myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
            myTimer.start();
            trace("the database was created successfully");
        }
        catch (error:SQLError)
        {
            trace("1 Error message:", error.message);
            trace("Details:", error.details);
        }

    }
    private function timerHandler(e:TimerEvent):void{
        if(!doesTableExist(conn,"employee")){
            createTable();
        }
    }

    public function updateTrack():void{

        insertStmt = new SQLStatement();
        insertStmt.sqlConnection = conn;
        var sql:String = 
            "INSERT INTO employee (name, tid) " + 
            "VALUES ('foo',2)";
        insertStmt.text = sql;                  
        insertStmt.addEventListener(SQLEvent.RESULT, insertResult);
        insertStmt.addEventListener(SQLErrorEvent.ERROR, insertError);
        insertStmt.execute();

    }

    public function getTracklist():void{
        trace("excuting select");
        selectStmt = new SQLStatement();            
        selectStmt.sqlConnection = conn;            
        selectStmt.text = "SELECT * FROM employee";             
        selectStmt.addEventListener(SQLEvent.RESULT, resultHandlernew);
        selectStmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        selectStmt.execute();
    }

    private function resultHandlernew(e:SQLResult):void{
        var result:SQLResult = selectStmt.getResult();          
        var numResults = result.data.length;
        trace(numResults);
    }

    private function createTable():void{
        var createStmt:SQLStatement = new SQLStatement();
        createStmt.sqlConnection = conn;

        var sql:String = 
            "CREATE TABLE IF NOT EXISTS employee (" + 
            "    id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            "    name TEXT, " + 
            "    tid INTEGER " +                
            ")";

        createStmt.text = sql;          
        createStmt.addEventListener(SQLEvent.RESULT, createResult);
        createStmt.addEventListener(SQLErrorEvent.ERROR, createError);          
        createStmt.execute();
    }

    function insertResult(event:SQLEvent):void
    {
        trace("INSERT statement succeeded");
        var result:SQLResult = insertStmt.getResult();
        var primaryKey:Number = result.lastInsertRowID;
        trace("primaryKey:"+primaryKey);
        getTracklist();
    }

    function insertError(event:SQLErrorEvent):void
    {
        trace("3 Error message:", event.error.message);
        trace("Details:", event.error.details);
    }


    public function doesTableExist(connection:SQLConnection, tableName:String):Boolean
    {
        var r:Boolean;
        try{
            connection.loadSchema();
            var schema:SQLSchemaResult = connection.getSchemaResult();

            for each (var table:SQLTableSchema in schema.tables)
            {
                if (table.name.toLowerCase() == tableName.toLowerCase())
                {
                    r= true;
                }
            }
            r = false;
        }catch(e){
            r = false;
        }
        return r;
    }

    private function createResult(event:SQLEvent):void
    {
        trace("Table created");
        updateTrack();
    }

    private function createError(event:SQLErrorEvent):void
    {
        trace("2 Error message:", event.error.message);
        trace("Details:", event.error.details);
    }

    private function openHandler(event:SQLEvent):void
    {
        trace("the database opened successfully");
    }

    private function errorHandler(event:SQLErrorEvent):void
    {
        trace("3 Error message:", event.error.message);
        trace("Details:", event.error.details);
    }
}
}
4

0 回答 0