首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在字符串中格式化python字符串

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

Stack Overflow用户
提问于 2018-11-24 06:50:00
回答 1查看 41关注 0票数 0

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

代码语言:javascript
复制
update inventory set checked_out = 'n' where item = 'dongle'

所有的项都放在一个游标中,这是我在循环中使用的:

代码语言:javascript
复制
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形成时,它形成为:

代码语言:javascript
复制
update inventory set checked_out = 'n' where item = dongle

因此,给出的错误如下:

代码语言:javascript
复制
_mysql_connector.MySQLInterfaceError: Unknown column 'dongle' in 'where clause'

如何在字符串中格式化字符串以使SQL正确执行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-24 06:55:34

只要添加引号,问题就出现了:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53455872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档