Python and Oracle Database Tutorial: Scripting for the Future

Python cx_Oracle logo

Contents

Overview

This tutorial is an introduction to using Python with Oracle Database. It contains beginner and advanced material. Sections can be done in any order. Choose the content that interests you and your skill level.

Follow the steps in this document. The tutorial directory has scripts to run and modify. The tutorial/solutions directory has scripts with the suggested code changes.

If you are new to Python review the Appendix: Python Primer to gain an understanding of the language.

Setup

These cx_Oracle tutorial instructions can be found here. The files used in them can be found in the cx_Oracle GitHub repository.

If you are running this tutorial in your own environment, install the required software:

  1. Python. Version 3.6 (or later) is preferred.

  2. cx_Oracle version 7.3, or version 8, or later.

  3. Oracle Client libraries.

  4. SQL*Plus such as from the Oracle Instant Client SQL*Plus Package.

The Advanced Queuing section requires Oracle client 12.2 or later. The SODA section requires Oracle client 18.5, or later, and Oracle Database 18 or later.

To create the schema run:

sqlplus sys/yoursyspassword@localhost/orclpdb1 as sysdba @sql/SetupSamples

If DRCP is not already running, connect to the SYS user again in SQL*Plus and execute the command:

execute dbms_connection_pool.start_pool()

Connection Information

The database connection information is set in two files:

The username is "pythonhol" with the password "welcome". The connect string is "localhost/orclpdb1". .

If your database is not local, or has a different service, you will need to modify the connection information in these two files. If you are installing your own schema, you can also modify the default username and password in sql/SampleEnv.sql.

The following sections may need adjusting, depending on how you have set up your environment.

Using Python cx_Oracle 7 with Oracle Database

Python is a popular general purpose dynamic scripting language. The cx_Oracle interface provides Python API to access Oracle Database.