Basic CRUD operations using cx_Oracle

In this series, we’re going to take a look at preforming CRUD (Create Retrieve Update Delete) operations using the cx_Oracle driver.

A good ORM will handle most of your needs.

An ORM tool can handle many of the repetitive processes when interfacing with a database.  Your project might call for an ORM tool such as SQLAlchemy or Pony.  No doubt about it,  a good ORM can come in very handy.  An ORM application will typically have a function for passing in raw SQL if needed, so you may not need to go straight to the driver.

Why learn to use the driver directly?

An ORM brings its own, different complexity to a project.  It may be overkill for some projects.  There are also times when a specific task is just different enough that an ORM may not be able to help, or its application to your requirements become so complex that your code becomes difficult to maintain.

And if you’re like me, its hard to be satisfied with a black box approach.  You want to know more about how your tools work and you want to have options, just in case.

Martin Fowler said “Mapping to a relational database involves lots of repetitive, boiler-plate code.  A framework that allows me to avoid 80% of that is worthwhile even if it is only 80%.”

When you have enough knowledge to implement direct CRUD operations, you are in a better position to choose the right tool for the right job.

Common Setup

All examples in this series will use the same database objects and connection information.

Creating the Database Objects

The following can be used to setup the initial tables we’ll use.  Please make sure you’re connected to a schema in which you can safely execute commands like these.

 Making the Connection
  1. Import the cx_Oracle driver.
  2. Import os module used to read the environment variable.
  3. Get the connection string from the environment variable.
  4. Create the connection object.

We will include this code section with all examples and use the “con” connection object “con” throughout the series.


To  cleanup the database when you are finished with the series, you just need to drop the two tables.  Please make sure you’re connected to the correct schema where you created the tables.

Guide to cx_Oracle CRUD Series

Initial Setup
Create records
Retrieve records
Update records
Delete records

3 thoughts on “Basic CRUD operations using cx_Oracle”

  1. Nice work! My only comment is that in addition to the warning about choosing a safe schema, you may want to add “alter session set current_schema = scott;”, just in case someone does a copy & paste of example code.

  2. Hi Blaine,
    i am trying to access oracle 11g DB using cx_Oracle module in python. your tutorial is very much helpful to start in this area, but i am getting “cx_Oracle.DatabaseError: ORA-21561: OID generation failed” . I am using python 2.7.10, MacOSX El-cap. cx_oracle installations went fine (with instantclient 12.1 basic,sdk and cx_Oracle 5.2.1) but i am stuck here. How ever i have tried the same in java with simple JDBC it is working fine.

    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *