2012-06-09 27 views
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]

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

回答

1

您在此处显示的代码没有任何问题 - 它正常工作。但是,createStatement将验证SQL代码,并在代码无效时抛出NS_ERROR_FAILURE错误。在你的情况下,表persons不存在,或者不存在所有的字段name,emailcity。您应该看看lastErrorString attribute以查看错误消息:

let statement = null; 
try 
{ 
    statement = conn.createStatement("INSERT INTO persons (name, email, city) VALUES (:name, :email, :city)"); 
} 
catch (e) 
{ 
    Components.utils.reportError(conn.lastErrorString); 
} 
相关问题