Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite3(如果存在)替代方案_Sqlite - Fatal编程技术网

sqlite3(如果存在)替代方案

sqlite3(如果存在)替代方案,sqlite,Sqlite,我正在尝试运行sqlite的简单查询,如果不存在记录,它将更新记录 我本可以使用Insert或replace,但是article\u标签表没有任何主键,因为它是一个关系表 如何为sqlite编写此查询,因为如果不存在不受支持。 我不知道如何使用这个? 表格结构: articles(id, content) article_tags(article_id, tag_id) tag(id, name) SQLITE尝试了不正确的语法 insert into article_tags (articl

我正在尝试运行sqlite的简单查询,如果不存在记录,它将更新记录

我本可以使用
Insert或replace
,但是
article\u标签
表没有任何
主键,因为它是一个关系表

如何为
sqlite
编写此查询,因为
如果不存在
不受支持。 我不知道如何使用这个

表格结构:

articles(id, content)
article_tags(article_id, tag_id)
tag(id, name)
SQLITE尝试了不正确的语法

insert into article_tags (article_id, tag_id ) values ( 2,7)
if not exists  (select 1 from article_tags where article_id =2 AND tag_id=7)

我认为正确的方法是在
article\u tags
表中添加一个主键,一个横跨两列的组合键。这是处理多对多关系表的正常方法

换句话说(伪DDL):


这样,重复对的插入将失败,而不必求助于
(如果不存在的话)

这似乎是可行的,尽管我会,正如其他人所说,建议您在表上添加一些约束,然后简单地“尝试”插入

insert into article_tags 
  select 2, 7 
  where  not exists ( select * 
                      from   article_tags 
                      where  article_id = 2 
                        and  tag_id     = 7 )

如果是关于sqlite3的,为什么要标记这个mysql?为什么不制作一个复合的唯一索引,并使用一个普通的
插入
?然后,当它已经存在时,您将得到一个错误,您可以捕获它。您不能基于这两列创建一个多列主键吗?好的,我已经验证了添加它的方法。创建此表后,“插入或替换到文章\u标记”会正常工作。谢谢,抱歉。我对答案做了一点修改。sqlite3似乎在不指定表的情况下也接受选择。现在更简单了。
insert into article_tags 
  select 2, 7 
  where  not exists ( select * 
                      from   article_tags 
                      where  article_id = 2 
                        and  tag_id     = 7 )