Oracle Tutorial

  • Home
  • Start Here
  • Basics
  • Advanced
    • Oracle View
    • Oracle Index
    • Oracle Synonym
    • Oracle Sequence
    • Oracle Administration
  • PL/SQL
  • Functions
    • Aggregate Functions
    • Analytic Functions
    • Comparison Functions
    • Date Functions
    • String Functions
  • API
    • Python Oracle
Home / Python Oracle / Updating Data In Oracle Database from Python

Updating Data In Oracle Database from Python

Summary: in this tutorial, you will learn how to use the Python cx_Oracle API to update data in a table.

To update data in a table, you follow these steps:

  • First, connect to the Oracle Database by creating a new Connection object.
  • Second, create a Cursor object from the Connection object.
  • Third, execute an UPDATE statement by calling the Cursor.execute() method.
  • Fourth, call the Connection.commit() method to apply the changes to the database.
  • Finally, release the Cursor and Connection objects. You can use the with block to release these objects automatically.

The following code illustrates how to update the amount for a billing document based on a specific billing number.

import cx_Oracle import config as cfg def update_billing(billing_no, amount): """ Update new amount for a billing :param billing_no: :param amount: :return: """ sql = ('update billing_headers ' 'set amount = :amount ' 'where billing_no = :billing_no') try: # establish a new connection with cx_Oracle.connect(cfg.username, cfg.password, cfg.dsn, encoding=cfg.encoding) as connection: # create a cursor with connection.cursor() as cursor: # execute the insert statement cursor.execute(sql, [amount, billing_no]) # commit the change connection.commit() except cx_Oracle.Error as error: print(error) if __name__ == '__main__': update_billing(1, 2000)
Code language: Python (python)

After executing the program, you can examine the contents of the billing_headers table:

SELECT * FROM billing_headers WHERE billing_no = 1;
Code language: Python (python)

Here is the output:

python oracle update example

In this tutorial, you have learned how to use the Python cx_Oracle API to update data in a table.

  • Was this tutorial helpful?
  • YesNo
Previous Inserting Data Into Table from Python
Next Deleting Data From Oracle Database in Python

Getting Started

  • What Is Oracle Database
  • Install Oracle Database Server
  • Download Oracle Sample Database
  • Create Oracle Sample Database
  • Connect To Oracle Database Server

Oracle Data Manipulation

  • SELECT
  • Oracle DUAL Table
  • ORDER BY
  • SELECT DISTINCT
  • WHERE
  • Table & Column Aliases
  • AND
  • OR
  • FETCH
  • BETWEEN
  • IN
  • LIKE
  • IS NULL
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
  • Self Join
  • GROUP BY
  • HAVING
  • UNION
  • INTERSECT
  • MINUS
  • GROUPING SETS
  • CUBE
  • ROLLUP
  • PIVOT
  • UNPIVOT
  • INSERT
  • INSERT INTO SELECT
  • INSERT ALL
  • UPDATE
  • DELETE
  • MERGE
  • Subquery
  • Correlated Subquery
  • EXISTS
  • NOT EXISTS
  • ANY
  • ALL

Oracle Data Types

  • Oracle Data Types
  • NUMBER
  • FLOAT
  • BINARY_FLOAT
  • CHAR
  • NCHAR
  • VARCHAR2
  • NVARCHAR2
  • DATE
  • INTERVAL
  • TIMESTAMP
  • TIMESTAMP WITH TIME ZONE

Oracle Data Definition

  • CREATE TABLE
  • Identity Column
  • ALTER TABLE
  • ALTER TABLE ADD Column
  • ALTER TABLE MODIFY Column
  • Drop Columns
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME Table
  • Oracle Virtual Column

Oracle Constraints

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • CHECK
  • NOT NULL

Oracle Views

  • CREATE VIEW
  • DROP VIEW
  • Updatable Views
  • Inline Views
  • WITH CHECK OPTION

About Oracle Tutorial

OracleTututorial.com website provides Developers and Database Administrators with the updated Oracle tutorials, scripts, and tips.

Search

Recent Tutorials

  • Oracle Implicit Statement Results
  • Calling PL/SQL Stored Functions in Python
  • Calling PL/SQL Procedures in Python
  • Managing Transaction in Python
  • Deleting Data From Oracle Database in Python

Site Links

  • Oracle Books
  • About
  • Contact
  • Privacy Policy
  • Terms of Use

Copyright © 2021 Oracle Tutorial. All Rights Reserved.