SQLAlchemy ON DUPLICATE KEY UPDATE

前端 未结 9 1446
死守一世寂寞
死守一世寂寞 2020-11-27 15:53

Is there an elegant way to do an INSERT ... ON DUPLICATE KEY UPDATE in SQLAlchemy? I mean something with a syntax similar to inserter.insert().execute(lis

相关标签:
9条回答
  • 2020-11-27 16:45

    It's depends upon you. If you want to replace then pass OR REPLACE in prefixes

      def bulk_insert(self,objects,table):
        #table: Your table class and objects are list of dictionary [{col1:val1, col2:vale}] 
        for counter,row in enumerate(objects):
            inserter = table.__table__.insert(prefixes=['OR IGNORE'], values=row)
            try:
                self.db.execute(inserter)
            except Exception as E:
                print E
            if counter % 100 == 0:
                self.db.commit()                    
        self.db.commit()
    

    Here commit interval can be changed to speed up or speed down

    0 讨论(0)
  • 2020-11-27 16:50

    Got a simpler solution:

    from sqlalchemy.ext.compiler import compiles
    from sqlalchemy.sql.expression import Insert
    
    @compiles(Insert)
    def replace_string(insert, compiler, **kw):
        s = compiler.visit_insert(insert, **kw)
        s = s.replace("INSERT INTO", "REPLACE INTO")
        return s
    
    my_connection.execute(my_table.insert(replace_string=""), my_values)
    
    0 讨论(0)
  • 2020-11-27 16:53

    I should mention that ever since the v1.2 release, the SQLAlchemy 'core' has a solution to the above with that's built in and can be seen under here (copied snippet below):

    from sqlalchemy.dialects.mysql import insert
    
    insert_stmt = insert(my_table).values(
        id='some_existing_id',
        data='inserted value')
    
    on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
        data=insert_stmt.inserted.data,
        status='U'
    )
    
    conn.execute(on_duplicate_key_stmt)
    
    0 讨论(0)
提交回复
热议问题