DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments

Trending

  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  1. DZone
  2. Data Engineering
  3. Databases
  4. Basic CRUD Operations Using cx_Oracle, Part 1: Setup

Basic CRUD Operations Using cx_Oracle, Part 1: Setup

In this series, we’re going to take a look at performing CRUD (Create Retrieve Update Delete) operations using the cx_Oracle driver. Read on to see the initial setup.

By 
Blaine Carter user avatar
Blaine Carter
·
Apr. 06, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

A Good ORM Will Handle Most of Your Deeds

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, it's 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.

CREATE TABLE cx_people (
 id NUMBER GENERATED BY DEFAULT AS identity,
 name VARCHAR2(20),
 age NUMBER,
 notes VARCHAR2(100)
)
/

ALTER TABLE CX_PEOPLE
ADD CONSTRAINT PK_CX_PEOPLE PRIMARY KEY ("ID")
/

CREATE TABLE CX_PETS (
 id NUMBER GENERATED BY DEFAULT AS IDENTITY,
 name VARCHAR2(20),
 owner NUMBER,
 type VARCHAR2(100)
)
/

ALTER TABLE CX_PETS ADD CONSTRAINT PK_CX_PETS PRIMARY KEY ("ID")
/

ALTER TABLE CX_PETS ADD CONSTRAINT FK_CX_PETS_OWNER FOREIGN KEY ("OWNER") REFERENCES "CX_PEOPLE" ("ID")
/

INSERT INTO cx_people (name, age, notes)
 VALUES ('Bob', 35, 'I like dogs')
/

INSERT INTO cx_people (name, age, notes)
 VALUES ('Kim', 27, 'I like birds')
/

INSERT INTO cx_pets (name, owner, type)
 VALUES ('Duke', 1, 'dog')
/

INSERT INTO cx_pets (name, owner, type)
 VALUES ('Pepe', 2, 'bird')
/

COMMIT
/

 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.
import cx_Oracle
import os
connectString = os.getenv('db_connect') 
con = cx_Oracle.connect(connectString)

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

Cleanup

To clean up 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.

drop table CX_PETS
/

drop table CX_PEOPLE
/

Guide to cx_Oracle CRUD Series

Initial Setup
Create records
Retrieve records
Update records
Delete records

Database

Published at DZone with permission of Blaine Carter. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook