Bulk update in SQLAlchemy Core using WHERE

后端 未结 3 818
北恋
北恋 2020-11-30 07:17

I have managed to work with the bulk insert in SQLAlchemy like:

conn.execute(addresses.insert(), [ 
   {\'user_id\': 1, \'email_address\' : \'jack@yahoo.com\         


        
3条回答
  •  一整个雨季
    2020-11-30 07:56

    Read Inserts, Updates and Deletes section of the documentation. Following code should get you started:

    from sqlalchemy.sql.expression import bindparam
    stmt = addresses.update().\
        where(addresses.c.id == bindparam('_id')).\
        values({
            'user_id': bindparam('user_id'),
            'email_address': bindparam('email_address'),
        })
    
    conn.execute(stmt, [
        {'user_id': 1, 'email_address' : 'jack@yahoo.com', '_id':1},
        {'user_id': 1, 'email_address' : 'jack@msn.com', '_id':2},
        {'user_id': 2, 'email_address' : 'www@www.org', '_id':3},
        {'user_id': 2, 'email_address' : 'wendy@aol.com', '_id':4},
    ])
    

提交回复
热议问题