不管返回什么行(with(out))指定列,除非定义了 SQLStatement 的 itemClass 属性,否则它将始终返回一个匿名对象。这基本上就是远程处理与 AMF 一起工作的方式。
您可以做两件事(取决于项目的复杂性):
- 指定一个 SQLStatement.itemClass - 这将使用与列名相同的名称的公共访问器(var 或 get/set)定义并填充返回的结果。
- 如果保留为匿名 - 列名称将附加到您在其中迭代对象的对象,就像定义它时一样。
超级基本的例子:
//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...
}
}