zoukankan      html  css  js  c++  java
  • _mysql_exceptions.IntegrityError: (1062, "Duplicate entry, Python操作MySQL数据库,插入重复数据

    [python] view plain copy

    1. sql = "INSERT  INTO test_c(id,name,sex)values(%s,%s,%s)"  
    2. param = (1,'AJ','MAN')  
    3. n = cursor.execute(sql,param)  
    4. db.commit() 
    当我们使用普通的 “INSERT INTO" 插入数据,如果数据有重复就会有报错:

    提示的是键值重复

    [python] view plain copy
    1. Traceback (most recent call last):  
    2.   File "D:/python/tongbu_py/test.py", line 14, in <module>  
    3.     n = cursor.execute(sql,param)  
    4.   File "D:Python27libsite-packagesMySQLdbcursors.py", line 174, in execute  
    5.     self.errorhandler(self, exc, value)  
    6.   File "D:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler  
    7.     raise errorclass, errorvalue  
    8. _mysql_exceptions.IntegrityError: (1062, "Duplicate entry

    INSERT IGNORE会忽略数据库中已经存在的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。这样就可以保留数据库中已经存在数据,达到在间隙中插入数据的目的

    REPLACE INTO 如果存在primary 或 unique相同的记录,则先删除掉。再插入新记录。

    You seem to be inserting constants into the database, not your actual values. Instead, try something similar to;

    db_query = cur.execute("INSERT INTO tblS100CurrentListing " +
        "(articleCode, dateReceived, s100RSD, remarks) VALUES (%s, %s, %s, %s)", 
        (articleCode, dateReceived, s100rsd, remark_text))

    What you want to search for is something called "SQLSTATE" - a set of standard error codes that cover most common RDBMS error states. They don't necessarily provide enough detail for all purposes though, and I don't know if sqlite supports them.

  • 相关阅读:
    three.js 制作一个三维的推箱子游戏
    three.js 郭先生制作太阳系
    three.js 制作魔方
    three.js 欧拉角和四元数
    mysql
    重装系统后需要安装的软件
    python3.7 安装PyQt5
    Java读取文件
    linux 环境jdk安装
    linux 修改用户字符集
  • 原文地址:https://www.cnblogs.com/timssd/p/6694493.html
Copyright © 2011-2022 走看看