• _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.

  • 相关阅读:
    HDFS文件操作(基本文件命令)
    <a> 标签
    css text-overflow
    zepto.js 打包自定义模块
    CSS3 box-sizing
    CSS3 Filter
    JQ 导出 Excel
    outline css2
    iphone 操作手势种类
    动手写一个简单的Web框架(模板渲染)
  • 原文地址:https://www.cnblogs.com/timssd/p/6694493.html
Copyright © 2020-2023  润新知