V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
filebolt
V2EX  ›  Python

Python SqlAlchemy 改写 sql 语句`update goods set monry = money + 10`

  •  
  •   filebolt · Sep 27, 2019 · 3794 views
    This topic created in 2409 days ago, the information mentioned may be changed or developed.
    • 改写成goods.money = models.Goods.money + goods_info["money"]
    • 上述语句,如果放到循环里,只会被执行最后一次循环的更新,原因是什么呢?有什么解决办法没有?
    6 replies    2019-09-27 18:02:29 +08:00
    nutting
        1
    nutting  
       Sep 27, 2019
    debug 一下
    CallMeReznov
        2
    CallMeReznov  
       Sep 27, 2019
    你 add 的吗?
    你不是 add 的每次循环中有 commit 吗?
    filebolt
        3
    filebolt  
    OP
       Sep 27, 2019
    @CallMeReznov 循环结束之后 commit 的,每次都 commit 不合适
    filebolt
        4
    filebolt  
    OP
       Sep 27, 2019
    是因为被操作被覆盖了吗?
    tonghuashuai
        5
    tonghuashuai  
       Sep 27, 2019
    ```python
    for i in range(10):
    goods_info = xxxxx
    goods.money = models.Goods.money + goods_info["money”]
    goods.save()
    ```
    zzlettle
        6
    zzlettle  
       Sep 27, 2019
    这就是为什么要学好数据库语言的重要性
    因为你只要学好 SQL 语法
    以后无论遇到什么框架
    什么语言
    什么 ORM
    你都能马上对付
    不用太多关心这些框架的 api 文档,很多这些框架
    搞得很复杂,api 设计的反人类
    但是他们肯定都会有 SQL 语言的支持
    否则你每次都要重新学
    还有各种 BUG 用各种技巧的面目跳出来
    你要是会 SQL
    直接用原始 sql
    到哪里都通用
    真的做到
    一次代码
    各种环境通用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2483 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 10:06 · PVG 18:06 · LAX 03:06 · JFK 06:06
    ♥ Do have faith in what you're doing.