Adobe Air: why SQLStatement's getResult().data

2019-09-09 12:34发布

问题:

Using Flash Builder 4.6, I am following http://www.flex-blog.com/adobe-air-sqlite-example (edit: link seems to be broken) as an example, and there is one part of codes that does not work:

private function resault(e:SQLEvent):void
{
    // with sqls.getResault().data we get the array of objects for each row out of our database
    var data:Array = sqls.getResult().data;
    // we pass the array of objects to our data provider to fill the datagrid
    dp = new ArrayCollection(data);
}

Checking the program during runtime gives me that sqls.getResult() returns a valid SQLResult object, but its data is null.

And from my previous question Adobe Air: convert sqlite's result [object Object] to String?, it seems I am asking the wrong question.

Nevertheless, I've checked my SQLResult object with

trace(ObjectUtil.toString(sqls.getResult()));

and I can see that I got all of my content from sqlite:

(flash.data::SQLResult)#0
  complete = true
  data = (Array)#1
    [0] (Object)#2
      first_name = "AAA"
      id = 1
      last_name = "BBB"
    [1] (Object)#3
      first_name = "AAA"
      id = 2
      last_name = "BBB"
    [2] (Object)#4
      first_name = "qqq"
      id = 3
      last_name = "qqq"
  lastInsertRowID = 0
  rowsAffected = 0

So what's going on here? Do I really have to create my own function to parse all of my sqlite elements and then place them in the data provider myself? Yes, I can do that, but seriously, many tutorials have shown using:

var data:Array = sqls.getResult().data;
dp = new ArrayCollection(data);

Now, back on the question: What might be the possible causes of sqls.getResult().data becoming null?

回答1:

That doesn't look like a very good tutorial you're following there (in my opinion). In that code, you have one event listener for all the statements that are being executed. It even has just one SQLStatement that executes different queries. I don't know exactly what is going wrong with your code, but I'm fairly certain the cause is to be found there. (And don't even get me started about that Timer used as a delay when a statement is still executing. Yuck!). I strongly suggest you look for a better source for learning Flex/AIR/SQLite.

You should simply create a new SQLStatement, or at least discrete event handlers for each Statement execution. A better way to do this, would be to use the Responder class, like this:

var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = connection;
stmt.text = query;

var token:Responder = new Responder(onResult, onFail);
stmt.execute(-1, token);

The SQLConnection can be shared though, if you don't mind keeping the connection to your database open all the time.