我目前正在尝试从sqlite中检索文本。我看到请求的数据量确实正确,但另一方面,内容似乎格式不正确。我尝试了一些转换:
var data:Array = sqls.getResult().data;
var stData:String = String(data[0]);
Alert.show(stData); // <--- displays "[object Object]"
字符串转换似乎没有做我想要的。我只想要sqlite数据库中的文本。在这种情况下,如何将[object Object]转换为正确的字符串?
答案 0 :(得分:4)
无论返回哪些行(带有(out))指定列,除非定义了SQLStatement的itemClass属性,否则它将始终返回一个匿名对象。这实质上是远程处理与AMF的结合方式。
您可以做两件事(取决于项目的复杂程度):
超级基本示例:
//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...
}
}
答案 1 :(得分:0)
请尝试使用Objectutil.toString();
函数将任何类型的对象转换为字符串。