V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wlxz
V2EX  ›  Python

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

  •  
  •   wlxz · 2017-12-02 02:10:05 +08:00 · 1390 次点击
    这是一个创建于 2402 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在可知参数情况下, 向 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 报错
    2 条回复    2017-12-06 12:53:13 +08:00
    wlxz
        1
    wlxz  
    OP
       2017-12-03 08:17:57 +08:00
    出发点为: 在不可知数 多查询条件下,怎么用 torndo 进行查询
    自己研究了一下,用**kwargs 方式解决了,
    wlxz
        2
    wlxz  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   933 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:41 · PVG 06:41 · LAX 15:41 · JFK 18:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.