Adobe Air:将 sqlite 的结果 [object Object] 转换为 String?

2021-11-12 00:00:00 string sqlite apache-flex air flash-builder

我目前正在尝试从 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):

  1. 指定一个 SQLStatement.itemClass - 这将定义 &使用与列名称相同的公共访问器(var 或 get/set)填充返回结果.
  2. 如果保留匿名 - 列名称将附加到您在其中迭代对象的对象,就像定义对象一样.

超级基础示例:

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...
      }
 }

相关文章