我用sql
格式化字符串sid
(注意:sid
的类型是str
)
sid = "34" #type(sid) is string, like 34, 34s
sql = '''SELECT * FROM table_name WHERE sid={sid}'''.format(sid=sid)
print sql
的结果是:
SELECT * FROM table_name WHERE sid=34
这超出了我的预期(34
vs "34"
):
SELECT * FROM table_name WHERE sid="34"
在将此sql
用作cur.execute(sql)
的参数时,遇到以下错误,导致错误输出。
Warning: Truncated incorrect DOUBLE value: '88s'
python大神给出的解决方案
如果要将值用引号引起来,可以将其添加到字符串中,并确保使用"
转义\
字符
>>> 'SELECT * FROM table_name WHERE sid=\"{sid}\"'.format(sid=sid)
'SELECT * FROM table_name WHERE sid="34"'