SQLite 中的 IF 语句:更新还是插入?
我无法使用 SQLite 运行此查询
if 0<(select COUNT(*) from Repetition where (Word='behnam' and Topic='mine'))
begin
update Repetition set Counts=1+ (select Counts from Repetition where (Word='behnam' and Topic='mine'))
end
else
begin
insert Repetition(Word,Topic,Counts)values('behnam','mine',1)
end
它显示“IF 附近的语法错误” 我该如何解决这个问题
I Can't run this query with SQLite
if 0<(select COUNT(*) from Repetition where (Word='behnam' and Topic='mine'))
begin
update Repetition set Counts=1+ (select Counts from Repetition where (Word='behnam' and Topic='mine'))
end
else
begin
insert Repetition(Word,Topic,Counts)values('behnam','mine',1)
end
It says "Syntax error near IF"
How can I solve the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQLite 没有 IF 语句(查看支持的查询列表)
Insetad,请查看ERIC B 对另一个线程的建议。您实际上正在考虑执行 UPSERT(如果记录存在则更新,如果不存在则插入)。 Eric B. 有一个很好的示例,说明如何利用 SQLite 中的“INSERT OR REPLACE”功能在 SQLite 语法中执行此操作。基本上,你会做类似的事情:
SQLite does not have an IF statement (see the list of supported queries)
Insetad, check out out ERIC B's suggestion on another thread. You're effectively looking at doing an UPSERT (UPdate if the record exists, INSERT if not). Eric B. has a good example of how to do this in SQLite syntax utilizing the "INSERT OR REPLACE" functionality in SQLite. Basically, you'd do something like:
另一种方法是 INSERT ... SELECT ... WHERE ... EXISTS [或 not] (SELECT ...);
我一直在做这种事情,并且我也使用 jklemmack 的建议。我这样做也有其他目的,例如在 UPDATE 中执行 JOIN(SQLite3 不支持)。
例如:
WHERE 中的条件与 IF 中的条件一样。 SELECT中的JOIN提供了SQLite3在UPDATE中不支持的JOIN。 INSERT OR REPLACE 和 t.id 的使用(如果 t 中不存在该行,则可以为 NULL)一起提供 THEN 和 ELSE 主体。
您可以反复应用此方法。如果您在 IF 的 THEN 部分中有三个语句(无法以某种方式合并为一个),则您需要在其 WHERE 中具有 IF 条件的三个语句。
Another approach is to INSERT ... SELECT ... WHERE ... EXISTS [or not] (SELECT ...);
I do this sort of thing all the time, and I use jklemmack's suggestion as well. And I do it for other purposes too, such as doing JOINs in UPDATEs (which SQLite3 does not support).
For example:
The WHERE there has the condition that you'd have in your IF. The JOIN in the SELECT provides the JOIN that SQLite3 doesn't support in UPDATE. The INSERT OR REPLACE and the use of t.id (which can be NULL if the row doesn't exist in t) together provide the THEN and ELSE bodies.
You can apply this over and over. If you'd have three statements (that cannot somehow be merged into one) in the THEN part of the IF you'd need to have three statements with the IF condition in their WHEREs.
这称为 UPSERT(即 UPdate 或 inSERT)。几乎所有类型的数据库都有其形式。查看 SQLite 版本的这个问题: SQLite - UPSERT *不是* INSERT 或 REPLACE
This is called an UPSERT (i.e. UPdate or inSERT). It has its forms in almost every type of database. Look at this question for the SQLite version: SQLite - UPSERT *not* INSERT or REPLACE
我发现的一种方法是基于 SQL
WHERE
子句true/false
语句:这实际上意味着执行一些
QUERIES 如果其他
QUERY
返回“任意”结果。One way that I've found is based on SQL
WHERE
clausetrue/false
statement:This actually means execute some
QUERIES
if some otherQUERY
returns 'any' result.