問題描述
我目前正在嘗試從 sqlite 檢索文本.我看到請求的數據量確實正確,但另一方面,內容的格式似乎不正確.我試過一些轉換:
I am currently trying to do retrieve text from sqlite. I see that the amount of data requested do come correctly, but content, on the other hand, seems to be in an incorrect format. I've tried some conversion:
var data:Array = sqls.getResult().data;
var stData:String = String(data[0]);
Alert.show(stData); // <--- displays "[object Object]"
字符串轉換似乎不是我想要的.我只想要來自 sqlite 數據庫的文本.在這種情況下,如何將 [object Object] 轉換為正確的字符串?
String conversion does not seem to do what I want. I simply want the text from the sqlite database. How can I convert the [object Object] to the correct string in this case?
推薦答案
無論返回什么行(with(out))指定列,除非定義了 SQLStatement 的 itemClass 屬性,否則它將始終返回一個匿名對象.這基本上就是遠程處理與 AMF 一起工作的方式.
Irrespective of what rows are returned (with(out)) specifying columns, unless the itemClass property of the SQLStatement is defined, it will always return an anonymous object. This is essentially how remoting works with AMF.
您可以做兩件事(取決于項目的復雜程度):
There are two things you can do (depending on the complexity of your project):
- 指定一個 SQLStatement.itemClass - 這將定義 &使用與列名稱相同的公共訪問器(var 或 get/set)填充返回結果.
- 如果保留匿名 - 列名稱將附加到您在其中迭代對象的對象,就像定義對象一樣.
超級基礎示例:
Super basic example:
//SQL table schema
CREATE TABLE accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
num INTEGER NOT NULL,
name TEXT NOT NULL,
last_update DATE
);
//Define an Account class:
public class Account {
public var id:int;
public var num:int;
public var name:String;
public var last_update:Date;
}
//A statement to execute to get all accounts returned as an array of "Account"
var statement:SQLStatement = new SQLStatement();
statement.sqlConnection = myConn;
statement.itemClass = Account;
statement.text = 'SELECT * FROM accounts';
statement.addEventListener(SQLEvent.RESULT, onResults);
statement.execute();
protected function onResults(event:SQLEvent):void
{
var statement:SQLStatement = SQLStatement(event.currentTarget);
var results:SQLResult = statement.getResult();
for each (var account:Account in results)
{
//do something useful like populate a model, grid, etc...
}
}
//Anonymous objects would iterate much the same way -when an item class isn't defined
protected function onResults(event:SQLEvent):void
{
var statement:SQLStatement = SQLStatement(event.currentTarget);
var results:SQLResult = statement.getResult();
for each (var account:Object in results)
{
//our 'Object' will have properties: id, num, name, last_update
//do something useful like populate a model, grid, etc...
}
}
這篇關于Adobe Air:將 sqlite 的結果 [object Object] 轉換為 String?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!