In Python, Using pyodbc, How Do You Perform Transactions?

限于喜欢 提交于 2019-11-29 10:48:50

By its documentation, pyodbc does support transactions, but only if the odbc driver support it. Furthermore, as pyodbc is compliant with PEP 249, data is stored only when a manual commit is done.
This means that you have to explicitely commit() the transaction, or rollback() the entire transaction.

Note that pyodbc also support autocommit feature, and in that case you cannot have any transaction.
By default, autocommit is off, but your codebase might have tuerned it on. You should check the connection, when it is performed

cnxn = pyodbc.connect(cstring, autocommit=True)

Alternatively, you can also explicitely turn off the autocommit mode with

cnxn.autocommit = False

but this might have quite a big impact on your system.

Note: you can get more information on the autocommit mode of pyodbc on its wiki

I don't think pyodbc has any specific support for transactions. You need to send the SQL command to start/commit/rollback transactions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!