我摆弄了psycopg2,虽然有一个.commit()和.rollback()没有.begin()或者类似的东西来启动一个事务,或者看起来好像?我希望能做到
db.begin() # possible even set the isolation level here
curs = db.cursor()
cursor.execute('select etc... for update')
...
cursor.execute('update ... etc.')
db.commit();
Run Code Online (Sandbox Code Playgroud)
那么,交易如何与psycopg2一起使用?我如何设置/更改隔离级别?
我在文档中读到:
...因为事务在游标执行查询时开始,但在Connection对象执行COMMIT或ROLLBACK时结束.
import MySQLdb
db = MySQLdb.connect(user="root", db="test")
c = db.cursor()
c.execute("SELECT * FROM books")
print c.fetchall()
Run Code Online (Sandbox Code Playgroud)
我怀疑MySQLdb甚至在不修改数据的查询(如SELECT)上启动事务,因为很难知道查询是否只读取数据而不写入数据.
cursor.commit()在每次查询后执行,以确保没有表被锁定?谢谢