python和mysql命令lin中的mysql插入错误

2024-09-29 23:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在mysql数据库中插入一些条目。在

当我在python脚本中这样做时,我得到了:

Error 1062: Duplicate entry '222' for key 'product_code'. product_code is an unique field.

当我在mysql命令行中执行此操作时,我得到:

Error 1205(HY000):Lock wait timeout exceed; try restarting transaction.

Mysql安装在win32上,表的引擎是innodb。在

代码:

conn = mdb.connect(user = 'root', passwd = '[REMOVED]', db = 'vancl')
cur = conn.cursor()

sql2 = "insert into vancl.vancl_query1(product_code) values('100000')"
print sql2
cur.execute(sql2)

cur.close()

Tags: key脚本数据库formysqlcode条目error
2条回答

这意味着product_code列上有一个唯一的索引约束。unique index约束告诉MySQL一个字段不能有重复的值,即每个字段必须唯一。当222值已经存在于列的某个位置时,尝试将值222插入到产品代码中时,会出现一个“重复项”错误。在

Now, I recreate a table the same as prev one. In script, no error was reported, but in actually no data is inserted. In command line, insert sql works perfect.

如果使用像InnoDB这样的事务引擎,则不会保存数据,因为MySqlDb默认为auto commit off。要将数据实际保存到数据库,必须调用commit

conn = mdb.connect(user = 'root', passwd = '[REMOVED]', db = 'vancl')
cur = conn.cursor()

sql2 = "insert into vancl.vancl_query1(product_code) values('100000')"
print sql2

cur.execute(sql2)

//Commit the data
conn.commit() 

cur.close()

相关问题 更多 >

    热门问题