【发布时间】:2017-08-27 01:18:27
【问题描述】:
我刚刚找到了用于将 Python 连接到 MySQL 数据库的 pymysql 模块。我有一个数据库,其中包含一个名为“loot”的表,loot 包含一个名为“wins”的列。我的代码包含一个名为“won”的变量,该变量在 SQL 行之前被赋予了一个值。我希望将变量“won”输入到 id=1 的“wins”列中。 id=1 行已存在于数据库中。
下面的代码抛出错误pymysql.err.InternalError: (1054, "Unknown column 'won' in 'field list'")
我的问题:为什么会出现此错误,我做错了什么?
守则:
import pymysql
# Open database connection
db = pymysql.connect(host='*******',user='******',password='*****',db='******')
# prepare a cursor object using cursor() method
cursor = db.cursor()
won=1
# Prepare SQL query to UPDATE required records
sql = "UPDATE loot SET wins = won WHERE id = 1"
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
# disconnect from server
db.close()
【问题讨论】: