怎么将 tuple 作为 torndb 查询的 params 传进去?

2017-12-02 02:10:05 +08:00
 wlxz
在可知参数情况下, 向 torndb 传递查询参数:
sql = 'select * from test where id=%s and name = %s '
row = conn.get(sql, 1,'admin')

# 需求 : 查询条件未知,如使用 dict
d = {
'name=': 'admin',
'id=': '1',
# .... 可能存在更多查询参数
}
where_str = '' # 这里可以拼接 d 的键名,生成 where 字符串
sql = 'select * from test ' + where_str
row = conn.get(sql, *args) # 这里的*args 怎么取到?

测试 将 d 的 value 放入 tuple 作为*args 传入,torndb 报错
1391 次点击
所在节点    Python
2 条回复
wlxz
2017-12-03 08:17:57 +08:00
出发点为: 在不可知数 多查询条件下,怎么用 torndo 进行查询
自己研究了一下,用**kwargs 方式解决了,
wlxz
2017-12-06 12:53:13 +08:00
原题答案如下:
def bar(*args):
print(type(args))
print(args)

t = ('1','2','3')
bar(*t)
参考:
https://stackoverflow.com/questions/5710391/converting-python-dict-to-kwargs

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/411292

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX