在 Javascript 函数中使用 last_insert_row() 与在数据库查询工具中使用时有何区别?
我在 Google Gears (Sqlite) 中创建了一个表:
db.execute('create table if not exists SPALINKS (link_id int PRIMARY KEY, sid1 int, sid2 int, label text, user text, Timestamp int');
当使用 Google Gears 数据库查询工具(在 FF 中)时,带有“last-insert-rowid”的 INSERT 工作正常。 只要我不使用“last-insert-rowid”,从 Javascript 插入就可以正常工作。 但是,当使用“last-insert-rowid”时,它不再起作用:
var sql_stmt = 'INSERT INTO SPALINKS (link_id, sid1, sid2, label, user, Timestamp) VALUES (last_insert_rowid(),?,?,?,?,?)';
var arg_array = [sid1, sid2, label, user, creation_time];
db.execute(sql_stmt, arg_array);
Error: Database operation failed. ERROR: constraint failed DETAILS: constraint failed
为什么 SQLite 的“last-insert-rowid”可以在 DB Tool 中正常工作,但不能在从 Javascript 函数内部创建和执行的 SQL 语句中正常工作?
I have created a table in Google Gears (Sqlite):
db.execute('create table if not exists SPALINKS (link_id int PRIMARY KEY, sid1 int, sid2 int, label text, user text, Timestamp int');
When using the Google Gears Database Query Tool (in FF), INSERT with 'last-insert-rowid' work fine. Inserts from Javascript work fine, as long a I don't use 'last-insert-rowid'. But when using 'last-insert-rowid' it doesn't work anymore:
var sql_stmt = 'INSERT INTO SPALINKS (link_id, sid1, sid2, label, user, Timestamp) VALUES (last_insert_rowid(),?,?,?,?,?)';
var arg_array = [sid1, sid2, label, user, creation_time];
db.execute(sql_stmt, arg_array);
Error: Database operation failed. ERROR: constraint failed DETAILS: constraint failed
Why does SQLite's 'last-insert-rowid' work fine with the DB Tool, but not in a SQL statement that is created and executed from inside a Javascript function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在这种情况下您不必使用
last_insert_rowid()
。 为什么只是让INTEGER NOT NULL PRIMARY KEY AUTOINCRMENT
。您可以在像
UPDATE
这样的情况下使用last_insert_rowid()
:我希望这会起作用。
I think that you do not have to use
last_insert_rowid()
in that situations. Why just makeINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
.You can use
last_insert_rowid()
in situation likeUPDATE
:I hope this is going to work.
问题似乎是相应的列没有定义为自动增量。
It seems that the problem was that the respective column wasn't defined as AUTOINCREMENT.