1

提供具有多个参数的语句的“最佳”方法是什么?我想在 SQLite 数据库中插入一行。

适应https://developer.mozilla.org/en/Storage#Binding_Parameters我认为以下方法可行:

var conn = Services.storage.openDatabase(dbfile); // Will also create the file if it does not exist
let statement  = conn.createStatement("INSERT INTO persons (name, email, city) VALUES (:name, :email, :city)");
statement.params.name = myName;
statement.params.email = myEmail;
statement.params.city = myCity;

但显然我什至无法创建语句本身:

组件返回失败代码:0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

之后我想异步执行它......

4

1 回答 1

1

There is nothing wrong with the code you show here - it works correctly. However, createStatement will validate the SQL code and throw an NS_ERROR_FAILURE error if that code is invalid. In your case either the table persons doesn't exist or not all of the fields name, email and city exist in it. You should take a look at the lastErrorString attribute to see the error message:

let statement = null;
try
{
  statement = conn.createStatement("INSERT INTO persons (name, email, city) VALUES (:name, :email, :city)");
}
catch (e)
{
  Components.utils.reportError(conn.lastErrorString);
}
于 2012-06-09T22:11:01.017 回答