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
crytis
V2EX  ›  Python

用 SQLAlchemy 的 session 怎么执行 update 语句

  •  
  •   crytis · 2016-10-11 15:25:41 +08:00 · 3632 次点击
    这是一个创建于 2754 天前的主题,其中的信息可能已经有所发展或是发生改变。

    sql 是这样的 UPDATE table_RP set send_times=send_times+1 WHERE id in (0,1,2,3),我的参数是后面 in 里面的,一个 list ,如何用 session 执行呢。我用了 session.execute(),试了好多次都不行

    3 条回复    2016-10-11 17:35:26 +08:00
    crytis
        1
    crytis  
    OP
       2016-10-11 15:48:42 +08:00 via iPhone
    目前先手动拼的 SQL😂
    lrh3321
        2
    lrh3321  
       2016-10-11 15:58:00 +08:00   ❤️ 1
    session.query(table_RP).filter(table_RP.id.in_((0,1,2,3))).update({table_RP.send_times: table_RP.send_times+1})

    类似这样的写法
    crytis
        3
    crytis  
    OP
       2016-10-11 17:35:26 +08:00
    @lrh3321 可以的 不过会报错 。在 update 里加上 synchronize_session='fetch' 这个就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2875 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:35 · PVG 11:35 · LAX 20:35 · JFK 23:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.