sqlite是否“插入或替换”?使用AUTOINCREMENT主键? [英] Does sqlite "insert or replace" work with AUTOINCREMENT primary key?

查看:172
本文介绍了sqlite是否“插入或替换”?使用AUTOINCREMENT主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中已创建一个名为 SectionDetails的表。
在此表中,我已将设置为具有 AUTOINCREMENT 属性的主键。我正在将数据插入此表中。

I have a database in which i have created one table with name "SectionDetails". In this table i have set "id" as a primary key with AUTOINCREMENT property.And i am inserting my data into this table.

但是,我来到这样一种情况,我需要检查要插入的记录是否已经存在(如果记录存在,则将其替换为相同的值或跳过它,如果记录是不存在,然后插入新的)。

However, i came to the scenario where i need to check if record i am inserting is already present or not(if record is present then replace it with same values or skip it,And if record is not present then insert new one ).

但是当我尝试插入具有相同列值的记录时,它将增加主键并再次插入同一行而不是替换。

But when i tried to insert record with same column values, it increases the primary key and insert the same row again instead of replacing.

所以,我的问题是-
sqlite的插入或替换是否可以与AUTOINCREMENT主键一起使用?

我正在使用以下查询:

insert or replace into SectionDetails(Name,Month,Title,Url)values('Deepak',"August","None","www.dd619.com")

此处未显示列 id,因为其主键具有 AUTOINCREMENT 属性。

here column "id" is not appearing because its a primary key with AUTOINCREMENT property.

推荐答案

简短的回答:不,不是。

Short answer: no it does not.

插入或替换仅在您还指定主键。使用您描述的设置中的自动增量,该方法将无法正常工作。

The "insert or replace" only works well when you also specify the primary key. With the autoincrement in a setup that you describe, this cannot work.

可能最快的方法是为用例创建自定义插入/更新方法。

Probably the quickest way to do that is by creating a custom insert/update method for your use case.

这篇关于sqlite是否“插入或替换”?使用AUTOINCREMENT主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆