我想知道我是否可以将上下文管理器with与Connection对象一起使用,并编写如下代码:
with MySQLdb.connect(...) as conn:
do_something()
Run Code Online (Sandbox Code Playgroud)
将conn物体像一个块后自动关闭file的对象?
谢谢.
我有一堆遵循这种模式的python方法:
def delete_session(guid):
conn = get_conn()
cur = conn.cursor()
cur.execute("delete from sessions where guid=%s", guid)
conn.commit()
conn.close()
Run Code Online (Sandbox Code Playgroud)
是否有更pythonic方式来执行原始SQL.每种方法开始和结束时的2行开始困扰我.
我不是在寻找一个orm,我想坚持使用原始sql.