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

sqlalchemy 的 insert or update

  •  
  •   fanhaipeng0403 · 2018-12-19 22:14:17 +08:00 · 2426 次点击
    这是一个创建于 1951 天前的主题,其中的信息可能已经有所发展或是发生改变。
      from sqlalchemy.ext.compiler import compiles
     import sqlalchemy.sql.expression as expr
     
     class Upsert(expr.Insert): pass
    
    
    @compiles(Upsert, "mysql")
    def compile_upsert(insert_stmt, compiler, **kwargs):
        if insert_stmt._has_multi_parameters:
            keys = insert_stmt.parameters[0].keys()
        else:
            keys = insert_stmt.parameters.keys()
        pk = insert_stmt.table.primary_key
        auto = None
        if (len(pk.columns) == 1 and
                isinstance(pk.columns.values()[0].type, sa.Integer) and
                pk.columns.values()[0].autoincrement):
            auto = pk.columns.keys()[0]
            if auto in keys:
                keys.remove(auto)
        insert = compiler.visit_insert(insert_stmt, **kwargs)
        ondup = 'ON DUPLICATE KEY UPDATE'
        updates = ', '.join(
            '%s = VALUES(%s)' % (c.name, c.name)
            for c in insert_stmt.table.columns
            if c.name in keys
        )
        if auto is not None:
            last_id = '%s = LAST_INSERT_ID(%s)' % (auto, auto)
            if updates:
                updates = ', '.join((last_id, updates))
            else:
                updates = last_id
        upsert = ' '.join((insert, ondup, updates))
        return upsert
         
         
         
         	try:
    
                ###INSERT OR UPDATe
                db.session.execute(Upsert( BIUserStatistic,rows))
            except:
                print('process_bi_user_statistic_records transaction.rollback()')
                db.session.rollback()
                raise
            else:
                print('process_bi_user_statistic_records transaction.commit()')
    
    8 条回复    2018-12-20 15:07:41 +08:00
    Kylin30
        1
    Kylin30  
       2018-12-19 23:50:28 +08:00
    我是谁?
    hellowang
        2
    hellowang  
       2018-12-20 08:34:54 +08:00
    我在哪?
    xpresslink
        3
    xpresslink  
       2018-12-20 10:38:34 +08:00
    我要到哪里去?
    JQZhang
        4
    JQZhang  
       2018-12-20 10:50:40 +08:00
    来 v2 几天我真是膨胀了,连 TM 标题都没看懂就敢进来看看
    fanhaipeng0403
        5
    fanhaipeng0403  
    OP
       2018-12-20 11:47:28 +08:00
    @Kylin30 就是 insert 不进去,就更新对应的行。。
    fanhaipeng0403
        6
    fanhaipeng0403  
    OP
       2018-12-20 11:47:34 +08:00
    @JQZhang 就是 insert 不进去,就更新对应的行。。
    fanhaipeng0403
        7
    fanhaipeng0403  
    OP
       2018-12-20 11:47:40 +08:00
    @xpresslink 就是 insert 不进去,就更新对应的行。。
    JQZhang
        8
    JQZhang  
       2018-12-20 15:07:41 +08:00
    @fanhaipeng0403 这个我是真不懂,python 超级白,还是等大神来给解答一下吧😂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4843 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:00 · PVG 18:00 · LAX 03:00 · JFK 06:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.