Python mysql 在插入之前检查重复项
表,
CREATE TABLE IF NOT EXISTS kompas_url
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
url VARCHAR(1000),
created_date datetime,
modified_date datetime,
PRIMARY KEY(id)
)
这是我尝试插入 kompas_url 表的
仅当 url 不存在时,还知道吗?
谢谢
here is the table
CREATE TABLE IF NOT EXISTS kompas_url
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
url VARCHAR(1000),
created_date datetime,
modified_date datetime,
PRIMARY KEY(id)
)
I am trying to do INSERT to kompas_url table only if url is not exist yet
any idea?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以先通过
url
进行SELECT
来确定它是否在其中,也可以使url
字段唯一:这将停止MySQL 不会插入重复行,但当您尝试插入时它也会报告错误。这不好——虽然我们可以处理这个错误,但它可能会掩盖其他错误。为了解决这个问题,我们使用 ON DUPLICATE KEY UPDATE 语法:
这允许我们在唯一字段中存在重复值的情况下提供 UPDATE 语句(这可以包括您的主键)。在这种情况下,我们可能希望用当前日期更新
modified_date
字段。编辑:按照~unutbu的建议,如果您不想更改重复,您可以使用 INSERT IGNORE 语法。它的工作原理如下:
这只是将某些类型的错误转变为警告 - 最有用的是,该错误表明将存在重复的唯一条目。如果您将关键字
IGNORE
放入语句中,则不会收到错误 - 查询将被简单地删除。在复杂的查询中,这也可能隐藏其他可能有用的错误,因此如果您想使用它,最好双重确保您的代码是正确的。You can either find out whether it's in there first, by
SELECT
ing byurl
, or you can make theurl
field unique:This will stop MySQL from inserting a duplicate row, but it will also report an error when you try and insert. This isn't good—although we can handle the error, it might disguise others. To get around this, we use the
ON DUPLICATE KEY UPDATE
syntax:This allows us to provide an
UPDATE
statement in the case of a duplicate value in a unique field (this can include your primary key). In this case, we probably want to update themodified_date
field with the current date.EDIT: As suggested by ~unutbu, if you don't want to change anything on a duplicate, you can use the
INSERT IGNORE
syntax. This simply works as follows:This simply turns certain kinds of errors into warnings—most usefully, the error that states there will be a duplicate unique entry. If you place the keyword
IGNORE
into your statement, you won't get an error—the query will simply be dropped. In complex queries, this may also hide other errors that might be useful though, so it's best to make doubly sure your code is correct if you want to use it.