如何格式化字符串以在 Python 中使用 mysqldb 进行查询? [英] How can I format strings to query with mysqldb in Python?
本文介绍了如何格式化字符串以在 Python 中使用 mysqldb 进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何正确执行此操作:
How do I do this correctly:
我想做这样的查询:
query = """SELECT * FROM sometable
order by %s %s
limit %s, %s;"""
conn = app_globals.pool.connection()
cur = conn.cursor()
cur.execute(query, (sortname, sortorder, limit1, limit2) )
results = cur.fetchall()
一切正常,但 %s %s 的顺序没有正确放入字符串.它将两个替换放在它们周围的引号中.
All works fine but the order by %s %s is not putting the strings in correctly. It is putting the two substitutions in with quotes around them.
所以结果是这样的:
ORDER BY 'somecol' 'DESC'
错误应该是:
ORDER BY somecol DESC
非常感谢任何帮助!
推荐答案
这篇关于如何格式化字符串以在 Python 中使用 mysqldb 进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文