在字符串中格式化 python 字符串

Formatting python string within a string

我正在使用 Python MySQL 连接器,需要编写一个 SQL 查询作为:

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。

加个引号好像是问题所在:

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()