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

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

  •  
  •   filebolt · 2019-09-27 11:13:37 +08:00 · 2955 次点击
    这是一个创建于 1665 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 改写成goods.money = models.Goods.money + goods_info["money"]
    • 上述语句,如果放到循环里,只会被执行最后一次循环的更新,原因是什么呢?有什么解决办法没有?
    6 条回复    2019-09-27 18:02:29 +08:00
    nutting
        1
    nutting  
       2019-09-27 11:14:55 +08:00
    debug 一下
    CallMeReznov
        2
    CallMeReznov  
       2019-09-27 11:23:29 +08:00
    你 add 的吗?
    你不是 add 的每次循环中有 commit 吗?
    filebolt
        3
    filebolt  
    OP
       2019-09-27 11:41:45 +08:00
    @CallMeReznov 循环结束之后 commit 的,每次都 commit 不合适
    filebolt
        4
    filebolt  
    OP
       2019-09-27 11:42:44 +08:00
    是因为被操作被覆盖了吗?
    tonghuashuai
        5
    tonghuashuai  
       2019-09-27 11:50:24 +08:00
    ```python
    for i in range(10):
    goods_info = xxxxx
    goods.money = models.Goods.money + goods_info["money”]
    goods.save()
    ```
    zzlettle
        6
    zzlettle  
       2019-09-27 18:02:29 +08:00
    这就是为什么要学好数据库语言的重要性
    因为你只要学好 SQL 语法
    以后无论遇到什么框架
    什么语言
    什么 ORM
    你都能马上对付
    不用太多关心这些框架的 api 文档,很多这些框架
    搞得很复杂,api 设计的反人类
    但是他们肯定都会有 SQL 语言的支持
    否则你每次都要重新学
    还有各种 BUG 用各种技巧的面目跳出来
    你要是会 SQL
    直接用原始 sql
    到哪里都通用
    真的做到
    一次代码
    各种环境通用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3020 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.