python連接mysql數據庫需注意的問題

系統環境:win + python3.3 + mysql.connector

使用import mysql.connector導入mysql數據庫的python接口庫,mysql.connetor手冊裏有如下內容:

1.Since by default, Connector/Python does not auto commit, it is possible to
cancel transactions when using transactional storage engines such as InnoDB.

使用innodb引擎時,autocommit是默認關閉的,如果在做更新,插入,刪除類操作時如果異常中斷,又沒有在exception里加入commit()或者rollback()進行處理,那麼寫鎖是沒有消除的,接着再重新運行程序就會因爲有鎖而被阻塞。

2.Since by default Connector/Python turns autocommit off, and MySQL 5.5 and later uses transactional InnoDB tables by default, it is necessary to commit your changes using the connection's commit()
method. You could also roll backusing the rollback()method。

設置autocommit的方法:cnx.autocommit = True



BTW:Oracle數據庫也同理,如果autocommit是關閉的,涉及到更新數據庫的操作如果異常中斷而沒有捕獲異常進行commit或者rollback處理,再次運行此程序更新數據庫將會被阻塞。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章