firefox扩展SQLite保存和获取
好吧,现在来说说多汁的东西。到目前为止,所有尝试都未能保存我的字符串。
以下是将其保存在 Firefox 扩展中的 sqllite 中的代码:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");
var storageService = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService);
var mDBConn = storageService.openDatabase(file);
mDBConn.execute("CREATE TABLE IF NOT EXISTS log_det (id INTEGER PRIMARY KEY AUTOINCREMENT, acc STRING)");
mDBConn.execute("INSERT INTO log_det (acc) VALUES(" + window['gluistr']+ ")");
mDBConn.drop();
以及用于检索值的代码:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");
var storageService = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService);
var mDBConn = storageService.openDatabase(file);
var res = mDBConn.execute("SELECT * FROM log_det");
mDBConn.drop();
不起作用。有人知道为什么吗? “执行”可以吗?还是我需要“createStatement”或“executeSimpleSQL”。我很困惑。
Ok now for the juicy stuff. All attempts failed to save my string so far.
Here is the code for saving it in sqllite in firefox extension:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");
var storageService = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService);
var mDBConn = storageService.openDatabase(file);
mDBConn.execute("CREATE TABLE IF NOT EXISTS log_det (id INTEGER PRIMARY KEY AUTOINCREMENT, acc STRING)");
mDBConn.execute("INSERT INTO log_det (acc) VALUES(" + window['gluistr']+ ")");
mDBConn.drop();
And the code for retrieving the value:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");
var storageService = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService);
var mDBConn = storageService.openDatabase(file);
var res = mDBConn.execute("SELECT * FROM log_det");
mDBConn.drop();
Is not working. Anybody knows why? Is "execute" ok or do I need "createStatement" or "executeSimpleSQL". I am confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用executeSimpleSQL。
openDatabase
返回一个 mozIStorageConnection 实例,该实例没有任何名为 <代码>执行。您可以在任何时候想要执行没有绑定参数的 SQL 语句时使用 executeSimpleSQL (这就是你正在做的)。您可能正在考虑 mozIStorageStatement
execute
方法。当需要绑定参数时,executeSimpleSQL
是不够的。相反,您需要创建一个语句,绑定任何参数,然后然后执行它:另请注意,
mozIStorageConnection
没有任何名为drop
的方法。也许您想编写mDBConn.close()
?所有这些都在这里:
Use
executeSimpleSQL
.openDatabase
returns a mozIStorageConnection instance, which does not have any method namedexecute
. You can use executeSimpleSQL any time you want to execute a SQL statement without bound parameters (which is what you're doing).You were probably thinking of mozIStorageStatement's
execute
method.executeSimpleSQL
is not sufficient when bound parameters are necessary. Instead, you need to create a statement, bind any parameters, and then execute it:Also note that
mozIStorageConnection
does not have any method nameddrop
. Maybe you meant to writemDBConn.close()
?All of this is covered here: