問題描述
使用 Flash Builder 4.6,我關注 http://www.flex-blog.com/adobe-air-sqlite-example (鏈接好像壞了) 舉個例子,還有一部分代碼這不起作用:
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);
}
在運行時檢查程序告訴我 sqls.getResult() 返回一個有效的 SQLResult 對象,但它的數據為空.
Checking the program during runtime gives me that sqls.getResult() returns a valid SQLResult object, but its data is null.
從我之前的問題 Adobe Air:將 sqlite 的結果 [object Object] 轉換為 String?,看來我是問錯了問題.
And from my previous question Adobe Air: convert sqlite's result [object Object] to String?, it seems I am asking the wrong question.
盡管如此,我已經用
trace(ObjectUtil.toString(sqls.getResult()));
并且我可以看到我從 sqlite 獲得了我的所有內容:
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
那么這里發生了什么?我真的必須創建自己的函數來解析我的所有 sqlite 元素,然后自己將它們放入數據提供程序中嗎?是的,我可以做到這一點,但說真的,許多教程都顯示使用:
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);
現在,回到問題:sqls.getResult().data 變為空的可能原因是什么?
Now, back on the question: What might be the possible causes of sqls.getResult().data becoming null?
推薦答案
這看起來不像是一個很好的教程(在我看來).在該代碼中,您有一個用于所有正在執行的語句的事件偵聽器.它甚至只有一個執行不同查詢的 SQLStatement.我不知道你的代碼到底出了什么問題,但我很確定原因可以在那里找到.(甚至不要讓我開始使用 Timer 在語句仍在執行時用作延遲.糟糕!).我強烈建議您尋找更好的資源來學習 Flex/AIR/SQLite.
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.
您應該簡單地創建一個新的 SQLStatement,或者至少為每個 Statement 執行創建離散的事件處理程序.一個更好的方法是使用 Responder 類,像這樣:
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);
不過可以共享 SQLConnection,如果您不介意始終保持與數據庫的連接.
The SQLConnection can be shared though, if you don't mind keeping the connection to your database open all the time.
這篇關于Adobe Air:為什么 SQLStatement 的 getResult().data 為空?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!