Why do I need to explicitly indicate when performing an UPDATE?

Here is my code:

import cx_Oracle

conn = cx_Oracle.connect(usr, pwd, url)
cursor = conn.cursor()
cursor.execute("UPDATE SO SET STATUS='PE' WHERE ID='100'")
conn.commit()

If I delete conn.commit(), the table will not be updated. But for selective statements I do not need this conn.commit(). I'm curious why?

+5
source share
3 answers

The DB-API specification requires a database connection to start a new transaction by default. You must commitconfirm all changes you have made or rollbackto cancel them.

Please note that if the database supports auto-commit, this should be disabled initially.

Pure SELECT, , .

+12

commit , .

, , ,

wikipedia

+6

Others explained why commit is not required in a SELECT statement. I just wanted to indicate that you can use the property autocommitof the Connection object to avoid having to manually commit:

import cx_Oracle

with cx_Oracle.connect(usr, pwd, url) as conn:
    conn.autocommit = True
    cursor = conn.cursor()
    cursor.execute("UPDATE SO SET STATUS='PE' WHERE ID='100'")
    cursor.close()

This is especially useful if there are several INSERT, UPDATE, and DELETE statements in the same connection.

0
source

All Articles