Python and Oracle Database 12c: Scripting for the Future

Python cx_Oracle logo

Contents

Preface

This tutorial was originally run as a 'Hands on Lab' at Oracle OpenWorld 2017. Attendees used a presupplied virtual machine preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a text editor, and access to a terminal console.

To set up a similar environment yourself, install the following required software:

  1. Python (3.6 preferred but 2.7 should work)
  2. cx_Oracle (6.2.1 preferred any 6.x should work) and Oracle Instant Client Package - Basic (12.2 preferred 12.1 should work)
  3. Oracle Instant Client Package - SQL*Plus OR Oracle SQLcl

To create the schema run:

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

Connection Information

The database connection information is set in two files:

The username is "pythonhol" with the password "welcome". The connect string is "localhost/orclpdb". See sql/SampleEnv.sql.

It is easist to have a local pluggable database with the service 'orclpdb' configured. If your database is not local, or has a different service, you will need to modify the connection information in db_config.py and db_config.sql.

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

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 home directory has scripts to run and modify. The solutions directory has scripts with the suggested code changes.

Use the Desktop icons to start editors and terminal windows.

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

Using Python cx_Oracle 6 with Oracle Database 12c

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