views:

415

answers:

1

Hi all,

I am devloping an editor with spell check feature in Flex + AIR using Sqlite as an embedded database. In database I have a table with the words. The table contains lacs of words. I have written the code to search for the word in table. I have used the sync method for all this. This pauses my application while searching. I would like to use async method to stop application pause.

The code for Search word is as follows:

    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;        
  }

If this function returns false(word not found) then i have to call the function to to red underline that word.

Please suggest me if I am going wrong. As I am using Sync method & it takes some milli seconds to search a word. & if I am wrting a paragraph then it makes my application sluggish.

Is there any other way I can store the words & search more fastly. If yes then please et me know.

Thanks in advance.

A: 

You'll need to reorganize your code a bit so that you call your function that does the red underline when the results come back instead of returning a Boolean from the method.

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();

}

Ben Johnson