为什么插入插入不执行?
数据库已打开,表已创建。但我无法插入记录,我正在使用 mozilla sqlite 存储:
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
let file = FileUtils.getFile("ProfD", ["filef.sqlite"]);
let dbconn = Services.storage.openDatabase(file);
dbconn.executeSimpleSQL('CREATE TABLE IF NOT EXISTS "quicklink" ("id" INTEGER PRIMARY KEY NOT NULL , "name" VARCHAR NOT NULL , "href" VARCHAR NOT NULL )');
var sqlinsert=dbconn.createStatement('INSERT INTO "quicklink" ("name","href") VALUES (:name,:href)');
sqlinsert.params.name = "Something";
sqlinsert.params.href = "URL";
dbconn.close();
我在 Firebug 控制台中没有收到任何错误,也没有在 try{}catch(){} 语句中收到任何错误。为什么?它有什么问题吗?
The database has opened, and the table has created. But I can't Insert record, I'm useing mozilla sqlite storage:
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
let file = FileUtils.getFile("ProfD", ["filef.sqlite"]);
let dbconn = Services.storage.openDatabase(file);
dbconn.executeSimpleSQL('CREATE TABLE IF NOT EXISTS "quicklink" ("id" INTEGER PRIMARY KEY NOT NULL , "name" VARCHAR NOT NULL , "href" VARCHAR NOT NULL )');
var sqlinsert=dbconn.createStatement('INSERT INTO "quicklink" ("name","href") VALUES (:name,:href)');
sqlinsert.params.name = "Something";
sqlinsert.params.href = "URL";
dbconn.close();
I don't get any error(s) in firebug console, or whit the try{}catch(){} statement. Why? What is the problem whit it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,您不能从客户端 Javascript 进行数据库访问。
有很多例外(例如,也许您在服务器上使用 Node.js,或者您可能在本地 Sqlite 数据库上使用 HP WebOS),但我没有在您的帖子中看到任何有关您的环境或数据库连接。
假设您可以在您的环境中使用 Javascript 合法连接到数据库,我看不到您在哪里执行准备好的 SQL 语句。
建议:
请指定您正在使用的数据库和数据库库
请向我们展示“执行”语句和相应的错误处理程序
请剪切/粘贴您在错误处理程序或数据库日志中收到的任何错误消息。
附录:
好的 - 我现在就和您在一起:
1) 您正在使用 Mozilla“存储”API。
2)您需要在“createStatement()”之后调用“executeStatement()”。
3)你还应该有一个“handleError”函数。
文档和示例代码位于:
http://developer.mozilla.org/En/Storage
In general, you CANNOT do database access from client-side Javascript.
There are many exceptions (for example, maybe you're using Node.js on a server, or maybe you're using HP WebOS on a local Sqlite database), but I didn't see anything in your post about your environment or database connectivity.
Assuming you CAN legitimately connect to a database with Javascript in your environment, I don't see where you're executing the prepared SQL statement.
Suggestions:
Please specify the database and database library you're using
Please show us the "execute" statement and corresponding error handler
Please cut/paste any error messages you're getting in the error handler or in the database log.
ADDENDUM:
OK - I'm with you now:
1) You're using the Mozilla "Storage" API.
2) You need to call "executeStatement()" after your "createStatement()".
3) You should also have a "handleError" function.
The documentation and sample code is here:
http://developer.mozilla.org/En/Storage
您使用什么数据库服务器?如果您使用的是 SQL Server,请使用探查器查看实际对 SQL Server 执行的命令并查看它生成的错误。
What database server are you using? If you're using SQL Server, use the profiler to see what command actually gets executed against SQL Server and see what the errors are that it generates.