2009-11-25 40 views
2

我使用Sqlite作爲嵌入式數據庫,使用Flex + AIR中的拼寫檢查功能對編輯器進行了devloping。 在數據庫中我有一個表格。該表包含詞彙。我寫了代碼來搜索表中的單詞。所有這些我都使用了同步方法。這會在搜索時暫停我的應用程序。 我想使用異步方法來停止應用程序暫停。Flex AIR Sqlite作爲嵌入式數據庫

的搜索詞的代碼如下:

public function searchInDictionary(word:String):Boolean 
    { 
    if(word == "") 
    return true; 
     connection= new SQLConnection(); 
     var query:SQLStatement = new SQLStatement(); 
     query.sqlConnection = connection; 
     query.text = 'SELECT id FROM tbl_'+ CommonLanguageCode +' WHERE word LIKE "'+word+'"';  

     try 
     {   
     connection.open(dbfile,SQLMode.READ); 
     } 
     catch(ex:Error) 
     {} 

     if(!connection.connected) 
     connection.open(dbfile); 
     query.execute(); 
     var result:SQLResult = query.getResult(); 
     if(result.data == null) 
     { 
     return false; 
     } 
     else 
     { 
     var numRows:uint = result.data.length; 
     var id:String; 
     if(numRows>0) 
      return true; 
     return false; 
     } 
     return false;   
    } 

如果該函數返回false(字未找到),然後我必須在函數調用,以紅色下劃線這個詞。

如果我出錯了,請給我建議。正如我使用同步方法&它需要幾毫秒來搜索一個字。如果我正在寫一段話,那麼它會讓我的申請呆滯。

是否有其他方式可以存儲文字&搜索更快。如果是的話請等我知道。

在此先感謝。

回答

1

您需要稍微重新組織代碼,以便在結果返回而不是從方法返回布爾值時調用您的函數,該函數執行紅色下劃線。

public function searchInDictionary(word:String):void 
{ 
    // Don't bother searching if no word was passed in 
    if(word == "") return; 

    // Open db connection asynchronously 
    var connection:SQLConnection = new SQLConnection(); 
    connection.openAsync(dbfile, SQLMode.READ); 

    // Create statement 
    var statement:SQLStatement = new SQLStatement(); 
    statement.sqlConnection = connection; 
    statement.text = 'SELECT id ' 
      + 'FROM tbl_'+ CommonLanguageCode +' ' 
      + 'WHERE word LIKE "' + word + '"'; 

    // Add event listener 
    statement.addEventListener(SQLEvent.RESULT, function(event:SQLEvent):void{ 
    var result:SQLResult = event.target.getResult(); 
    if(result.data.length == 0) { 
     // ... call method to red underline word in text ... 
    } 
    }); 

    // Execute statement 
    statement.execute(); 

}