我试图使用MySQL文档中提供的python连接器代码,并在已经创建的小型数据库上对其进行测试,但是该数据库中止了。该代码仅应连接到数据库并添加新的电子邮件地址。
import mysql.connector
from mysql.connector import errorcode
cnx = mysql.connector.connect(user='root', password='pwd', host='localhost', database='db')
cursor = cnx.cursor()
add_email = ("INSERT INTO employee(MID, Email) VALUES (%s,%s)")
email_details = (NULL, "[email protected]")
cursor.execute(add_email, email_details)
cnx.commit()
input("data entered successfully")
cnx.close()
通过设置断点,我发现问题可能出在cursor.execute()
语句中。(%s
由于MID是自动递增,因此我使用Null作为第一个)
要解决此问题NULL
(对于自动递增的“ MID”),请替换为None
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句