0

我真的整天都在努力让 Firefox 服从我的意志......

我想 :

int c = SELECT COUNT(*) FROM ...

我试过executeAsync({...});了,但我认为这是错误的范例,因为我想要立即得到结果。(并mozIStoragePendingStatement导致错误)

var count = 0;
var conn = Services.storage.openDatabase(dbfile); // Will also create the file if it does not exist
let statement = conn.createStatement("SELECT COUNT(*) FROM edges LIMIT 42;");
console.log("columns: " + statement.columnCount);  // prints "1";
console.log("col name:" + statement.getColumnName(0)); // is "COUNT(*)"

while (statement.executeStep())
    count = statement.row.getResultByIndex(0); // "illegal value"
    count = statement.row.getString(0); // "illegal value", too
    count = statement.row.COUNT(*); // hahaha. still not working
    count = statement.row[0]; // hahaha. "undefinded"
    count = statement.row[1]; // hahaha. "undefinded"
}
statement.reset();

它基本上可以工作,但我没有得到价值。所有语句(循环内的语句)有什么问题。

感谢您的任何提示...

4

2 回答 2

1

我试过executeAsync({...});了,但我认为这是错误的范例,因为我想要立即得到结果。

您不应该这样,Storage API 是异步的是有原因的。对数据库的同步访问可能会导致随机延迟(例如,如果硬盘驱动器繁忙)。而且由于您的代码在主线程(为用户界面提供服务的同一线程)上执行,因此在您的代码等待数据库响应时,整个用户界面将挂起。Mozilla 开发人员在 Firefox 3 中尝试了同步数据库访问,并很快注意到它降低了用户体验 - 因此使用异步 API,数据库处理发生在后台线程上,不会阻塞任何东西。

您应该更改代码以异步工作。例如,应该这样做:

Components.utils.import("resource://gre/modules/Services.jsm");

var conn = Services.storage.openDatabase(dbfile);
if (conn.schemaVersion < 1)
{
  conn.createTable("edges", "s INTEGER, t INTEGER");
  conn.schemaVersion = 1;
}

var statement = conn.createStatement("SELECT COUNT(*) FROM edges");
statement.executeAsync({
  handleResult: function(resultSet)
  {
    var row = resultSet.getNextRow();
    var count = row.getResultByIndex(0);
    processResult(count);
  },
  handleError: function(error) {},
  handleCompletion: function(reason) {}
});

// Close connection once the pending operations are completed
conn.asyncClose();

另请参阅:mozIStorageResultSetmozIStorageRow

于 2012-06-15T05:48:45.947 回答
1

尝试别名计数(*)作为总数,然后获取

于 2012-06-15T06:04:51.147 回答