Python - Delete Records From Table in Oracle

In this tutorial, I am giving a Python program example to delete records from the table in Oracle.

Delete from Oracle Table Using Python

The following example uses the cx_Oracle library to connect to Oracle Database from Python and delete the record from the EMP table of SCOTT schema.

import cx_Oracle

con = cx_Oracle.connect('scott/tiger@localhost:1521/orcl')

def delete_emp(n_empno):
    cur = con.cursor()
    cur.execute("Delete from EMP where empno = :n_empno",
                {'n_empno': (n_empno)})
    if cur.rowcount > 0:
        print('Employee record deleted successfully.')
    else:
        print('Delete operation failed.')
    cur.close()
    con.commit()
    con.close()

# call the delete_emp function by passing a employee id
try:
    delete_emp(7521)
except Exception as e:
    print(e)

Output

Employee record deleted successfully.

See also:

Leave a Comment