在 mysql-python 中使用游标我曾经调用“BEGIN;”、“COMMIT;”和“ROLLBACK;” 明确如下:
try:
cursor.execute("BEGIN;")
# some statements
cursor.execute("COMMIT;")
except:
cursor.execute("ROLLBACK;")
然后,我发现底层的连接对象有相应的方法:
try:
cursor.connection.begin()
# some statements
cursor.connection.commit()
except:
cursor.connection.rollback()
检查DB-API PEP我发现它没有提到连接对象的 begin() 方法,即使是扩展也是如此。
顺便说一句,当您使用该方法时,Mysql-python 会抛出 DeprecationWarning。例如 sqlite3.connection 根本没有该方法。
问题是为什么 PEP 中没有这种方法?该语句是否以某种方式可选,是否足以代替调用 commit() ?