Sqlite.房子的查询

Sqlite.jsm queries

本文关键字:查询 房子 Sqlite      更新时间:2023-09-26

我正在尝试使用sqlite。jsm而不是mozStorage。这里是我的原始代码文件。

var sql = "UPDATE row SET color=?1 WHERE id=?2";
  var statement = connection.createAsyncStatement(sql);
  statement.bindInt32Parameter(0, row.color);
  statement.bindInt32Parameter(2, row.id);
  statement.executeAsync();

新代码:

    Components.utils.import("resource://gre/modules/Sqlite.jsm");
    Components.utils.import("resource://gre/modules/Task.jsm");
    Task.spawn(function() {
      var db;
      try {
    // Open a database
    db = yield Sqlite.openConnection({ path: "file.sqlite" });
    var sql = "UPDATE row SET color=?1 WHERE id=?2";
    var dataToInsert = [
      ["color", row.color],
      ["id", row.id],
    for (var data of dataToInsert) {
      yield db.execute(sql, data);
    }
  } catch (ex) {
    // **Here i get :  Error(s) encountered during statement execution.**
  } finally {
    if (db) {
      yield db.close();
    }
  }
});

那么如何在UPDATE查询中使用sqlite传递参数。房子吗?提前感谢

对于提问的人来说可能有点晚了,但仍然可能有用。

Sqlite。JSM鼓励使用带有命名参数和对象的查询作为参数存储,所以正确的代码应该是这样的:

Components.utils.import("resource://gre/modules/Sqlite.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
return Task.spawn(function*() {
    var db;
    try {
        // Open a database
        db = yield Sqlite.openConnection({path: "file.sqlite"});
        var sql = "UPDATE row SET color = :color WHERE id = :id";
        var params = {
            'color': row.color,
            'id': row.id
        };
        return db.execute(sql, params);
    } catch (ex) {
     // Exception handling
    } finally {
        if (db) {
            yield db.close();
        }
    }
});