【发布时间】:2020-11-27 05:57:27
【问题描述】:
我正在尝试将我的数据插入到我已经创建的数据库中。我已将架构提供给 db,因此它接受 varchar 和 text 用于适当的列,并接受 date/integer 用于其他两个。我能够连接到我的数据库,但我正在与错误消息作斗争,并且我的代码根据我的格式继续失败。如果有人可以指导如何改变这种情况,我将不胜感激! (我已经注释掉了try/error,只是为了在运行代码时得到错误信息输出。)
#open db connection
db = mysql.connector.connect(host='localhost', user='root', database='WebScraping')
print(db)
#prepare a cursor object using cursor() method
cursor = db.cursor()
#prepare SQL query to INSERT a record into the database
sql = "INSERT INTO WebScraping.Reviews(name, location, date, rating, content) VALUES ('{}', '{}', '{}', '{}', '{}')".format(name, location, date, rating, content)
#try:
#execute the SQL command
cursor.execute(sql)
#commit your changes in the database
db.commit()
print("Record committed")
#except:
#Rollback if there is an error
#db.rollback()
#disconnect from the server
cursor.close()
db.close()
【问题讨论】:
标签: mysql mysql-python mysql-error-1064