我正在使用Python连接器,需要编写一个MySQL查询如下:
update inventory set checked_out = 'n' where item = 'dongle'所有的项都放在一个游标中,这是我在循环中使用的:
cnx = mysql.connector.connect(host='localhost',database='mem_bug',user='root',password='July@2013')
for r in cursor :
sql = "update inventory set checked_out = 'n' where item = {}".format(r[0])
cursor.execute(sql)
cnx.commit()当SQL形成时,它形成为:
update inventory set checked_out = 'n' where item = dongle因此,给出的错误如下:
_mysql_connector.MySQLInterfaceError: Unknown column 'dongle' in 'where clause'如何在字符串中格式化字符串以使SQL正确执行。
发布于 2018-11-24 06:55:34
只要添加引号,问题就出现了:
cnx = mysql.connector.connect(host='localhost',database='mem_bug',user='root',password='July@2013')
for r in cursor :
sql = "update inventory set checked_out = 'n' where item = '{}'".format(r[0])
cursor.execute(sql)
cnx.commit()https://stackoverflow.com/questions/53455872
复制相似问题