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

sqlalchemy 使用 session 修改数据库如何知道变化条目数?

  •  
  •   aiqier · 2015-09-07 16:00:23 +08:00 · 2312 次点击
    这是一个创建于 3158 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在使用 mysqldb 这样的库时,可以使用游标来查看数据库发生变化的条目数。

    cur = con.cursor ()
    cur.execute ("DELETE  FROM Writers WHERE created_time <= %s", (datetime,))
    numrows = int (cur.rowcount )
    

    那如果是使用 sqlalchemy 的 session 操作数据库呢?

    self.session.query (Writer ).filter (Writer.created_time <=  datetime,).delete ()
    

    我如何知道到底删了多少条记录?

    3 条回复    2015-09-07 23:21:53 +08:00
    neoblackcap
        1
    neoblackcap  
       2015-09-07 16:21:21 +08:00   ❤️ 1
    不是会返回一个成功执行的条数么?
    aiqier
        2
    aiqier  
    OP
       2015-09-07 16:27:51 +08:00
    @neoblackcap 谢了啊,搜了半天没搜到。
    zhuangzhuang1988
        3
    zhuangzhuang1988  
       2015-09-07 23:21:53 +08:00
    看下 delete 文档.
    :return: the count of rows matched as returned by the database's
    "row count" feature.
    http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html#sqlalchemy.orm.query.Query.delete
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2251 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 04:31 · PVG 12:31 · LAX 21:31 · JFK 00:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.