Avoiding boilerplate session handling code in sqlalchemy functions

前端 未结 3 1343
暗喜
暗喜 2020-12-25 12:50

I have a python application which has lots of small database access functions, using sqlalchemy. I\'m trying to avoid having lots of boilerplate session handling code around

3条回答
  •  时光取名叫无心
    2020-12-25 13:40

    transaction handling (begin, commit/rolllback) using with clause

    with engine.begin() as connection:
        r1 = connection.execute(table1.select())
        connection.execute(table1.insert(), {"col1": 7, "col2": "this is some data"})
    

    Old questions, but I still stumbled upon it so here is the relevant link from the docu: https://docs.sqlalchemy.org/en/13/core/connections.html

提交回复
热议问题