SQLite 中的 IF 语句:更新还是插入?

发布于 2024-12-11 09:07:03 字数 363 浏览 0 评论 0原文

我无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会傲 2024-12-18 09:07:03

SQLite 没有 IF 语句(查看支持的查询列表

Insetad,请查看ERIC B 对另一个线程的建议。您实际上正在考虑执行 UPSERT(如果记录存在则更新,如果不存在则插入)。 Eric B. 有一个很好的示例,说明如何利用 SQLite 中的“INSERT OR REPLACE”功能在 SQLite 语法中执行此操作。基本上,你会做类似的事情:

INSERT OR REPLACE INTO Repetition (Word, Topic, Counts)    
VALUES (  'behnam', 'mine',
          coalesce((select Counts + 1 from Repetition 
                   where Word = 'behnam', AND Topic = 'mine)
                   ,1)
       )

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 OR REPLACE INTO Repetition (Word, Topic, Counts)    
VALUES (  'behnam', 'mine',
          coalesce((select Counts + 1 from Repetition 
                   where Word = 'behnam', AND Topic = 'mine)
                   ,1)
       )
庆幸我还是我 2024-12-18 09:07:03

另一种方法是 INSERT ... SELECT ... WHERE ... EXISTS [或 not] (SELECT ...);

我一直在做这种事情,并且我也使用 jklemmack 的建议。我这样做也有其他目的,例如在 UPDATE 中执行 JOIN(SQLite3 不支持)。

例如:

CREATE TABLE t(id INTEGER PRIMARY KEY, c1 TEXT NOT NULL UNIQUE, c2 TEXT);
CREATE TABLE r(c1 TEXT NOT NULL UNIQUE, c2 TEXT);
INSERT OR REPLACE INTO t (id, c1, c2)
  SELECT t.id, coalesce(r.c1, t.c1), coalesce(r.c2, t.c2)
  FROM r LEFT OUTER JOIN t ON r.c1 = t.c1
  WHERE r.c2 = @param;

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:

CREATE TABLE t(id INTEGER PRIMARY KEY, c1 TEXT NOT NULL UNIQUE, c2 TEXT);
CREATE TABLE r(c1 TEXT NOT NULL UNIQUE, c2 TEXT);
INSERT OR REPLACE INTO t (id, c1, c2)
  SELECT t.id, coalesce(r.c1, t.c1), coalesce(r.c2, t.c2)
  FROM r LEFT OUTER JOIN t ON r.c1 = t.c1
  WHERE r.c2 = @param;

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.

瑶笙 2024-12-18 09:07:03

这称为 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

丘比特射中我 2024-12-18 09:07:03

我发现的一种方法是基于 SQL WHERE 子句 true/false 语句:

 SELECT * FROM SOME_TABLE
 WHERE
 ( 
    SELECT SINGLE_COLUMN_NAME FROM SOME_OTHER_TABLE
    WHERE 
    SOME_COLUMN = 'some value' and 
    SOME_OTHER_COLUMN = 'some other value'
  )

这实际上意味着执行一些QUERIES 如果其他 QUERY 返回“任意”结果。

One way that I've found is based on SQL WHERE clause true/false statement:

 SELECT * FROM SOME_TABLE
 WHERE
 ( 
    SELECT SINGLE_COLUMN_NAME FROM SOME_OTHER_TABLE
    WHERE 
    SOME_COLUMN = 'some value' and 
    SOME_OTHER_COLUMN = 'some other value'
  )

This actually means execute some QUERIES if some other QUERY returns 'any' result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文