简体   繁体   English

SQLSTATE [23000]:完整性约束违反错误处理

[英]SQLSTATE[23000]: Integrity constraint violation error handling

I know what the error means, and why it's occurring, meaning this is not a duplicate of the many topics covering State 23000. I'm using PDO to insert a duplicate record into a table that has a compound PK. 我知道错误的含义以及发生的原因,这意味着这不是涉及State 23000的许多主题的重复。我正在使用PDO将重复记录插入具有复合PK的表中。 I did this on purpose, in order to automatically cancel the insertion of any duplicated records in to this specific table. 我故意这样做是为了自动取消在此特定表中插入任何重复的记录。

The error, SQLSTATE[23000]: Integrity constraint violation , is killing the script. 错误SQLSTATE[23000]: Integrity constraint violation ,正在杀死脚本。 Is there a way to set the warn level for this particular error, to literally tell it to shut up and carry on? 有没有一种方法可以设置此特定错误的警告级别,从字面上告诉它关闭并继续进行?

I don't understand the purpose of having compound PKs if we can't allow them to work for us? 如果我们不能让复合PK为我们工作,我不明白拥有复合PK的目的吗?

I'm not very experienced with DB administration so you'll have to forgive me if I come across a bit 'it should work if I hit it with a hammer'. 我对数据库管理的经验不是很丰富,所以如果我遇到一点“如果我用锤子敲打它应该会起作用”,您将不得不原谅我。

Table Structure: 表结构:

CREATE TABLE `cart` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `userid` int(10) NOT NULL,
 `itemid` int(10) NOT NULL,
 UNIQUE KEY `id` (`id`),
 UNIQUE KEY `unique_records` (`userid`,`itemid`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1

Note the compound PK called 'unique records'. 注意复合PK,称为“唯一记录”。

so I have this table: 所以我有这张桌子:

| id | userid | itemid |
|  1 |    175 |     12 |

And I execute this query: 然后执行以下查询:

    $insertItem = $db->query("
                              INSERT INTO cart (userid, itemid) 
                              VALUES (:userid, :itemid), 
                              array("userid"=>"175", "itemid"=>"12")
                            ");

Executing this query prompts this error: SQLSTATE[23000]: Integrity constraint violation . 执行此查询将提示以下错误: SQLSTATE[23000]: Integrity constraint violation

I fully understand why I'm getting this error - I just don't understand why it's killing my submission? 我完全理解为什么会收到此错误-我只是不明白为什么它会杀死我的提交? I thought it should just keep working ignoring the fact that a few records weren't inserted because they were duplicates? 我认为它应该继续工作,而忽略一些记录因为重复而没有插入的事实?

Advice appreciated! 咨询表示赞赏!

If you want the insert to keep working, you can try using INSERT IGNORE : 如果您希望insert继续工作,可以尝试使用INSERT IGNORE

INSERT IGNORE INTO cart(userid, itemid) 
    VALUES (:userid, :itemid);

INSERT IGNORE can be a bit strong because it ignores all errors. INSERT IGNORE可能会很强大,因为它会忽略所有错误。 If you just want to ignore duplicate key errors, use on duplicate key and set a value to itself: 如果只想忽略重复键错误,请on duplicate key使用并为其设置一个值:

INSERT IGNORE INTO cart(userid, itemid) 
    VALUES (:userid, :itemid)
    ON DUPLICATE KEY userid = values(userid);

The set part does nothing -- the value doesn't change -- but the effect is to ignore the error. set部分不执行任何操作-值不变-但结果是忽略该错误。

Your question appears to be quite vague - either you want to ignore your integrity constraint violations, or you want them to be a signal saying "no more inserts". 您的问题似乎很模糊-您想忽略完整性约束违规,还是希望它们成为“不再插入”的信号。

In addition to the answer with insert ignore solution - when you execute your query and get SQLSTATE[23000]: Integrity constraint violation error, that means PDO throws exception. 除了使用insert ignore解决方案的答案外,当您执行查询并获得SQLSTATE[23000]: Integrity constraint violation错误时,这意味着PDO引发异常。 If you don't catch it, your script is killed. 如果您不catch它,则脚本将被杀死。 If you don't want it to be killed, wrap your DB code into try-catch construct, ie: 如果您不希望它被杀死,请将您的数据库代码包装到try-catch构造中,即:

try {
    // code here
    $insertItem = $db->query("
                              INSERT INTO cart (userid, itemid) 
                              VALUES (:userid, :itemid)", 
                              array("userid"=>"175", "itemid"=>"12")
                            );
    // more code here
} catch(Exception $e) {
    // handle exception - 
    // find out if it is caused by integrity contraints violations
    // and if it is - merely go further
    // otherwise do something else, like re-throwing your exception
}

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM