Python MySQLdb字符串替换而不添加引号 [英] Python MySQLdb string substitution without added quotations

查看:100
本文介绍了Python MySQLdb字符串替换而不添加引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对逗号分隔的列表使用字符串替换,例如:

I'd like to use string substitution for a comma-separated list, such as:

query = """SELECT id, name, image_id
           FROM users
           WHERE id IN (%s)
           """
values = (','.join(uids))
results = dbc.getAll(query, values

这将执行查询:

SELECT id, name, image_id
FROM users
WHERE id IN ('1,2,3')

哪个无法正常工作.

如何进行订阅,以便获得不带引号的查询,例如:

How can I do a substution so that I get the query without the quotations, such as:

SELECT id, name, image_id
FROM users
WHERE id IN (1,2,3)

推荐答案

让MySQLdb进行整个参数替换.在这种情况下,预期参数values应该是序列(1,2,3),而不是字符串'1,2,3':

Let MySQLdb do the entire argument substitution. In this case, the expected argument, values, should be the sequence (1,2,3), rather than the string '1,2,3':

query = """SELECT id, name, image_id
           FROM users
           WHERE id IN %s
           """    
values = uids
results = dbc.getAll(query, [values])

这篇关于Python MySQLdb字符串替换而不添加引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆