[PYTHON] [Introduction to cx_Oracle] Overview of cx_Oracle

What is cx_Oracle

cx_Oracle is a module for SQL access to Oracle Database from Python. Created by Oracle. It is designed to be as compliant as possible with PEP 249 (Python Database API Specification v2.0). The version of cx_Oracle as of June 1, 2020 (initially published in this article) is 7.3. It runs on Python 2.7 or later, or Python 3.5 or later. The license is the BSD license. The DB access part uses ODPI-C (Oracle Database Programming Interface for C), an Oracle Database access library for OSS C language provided by Oracle, instead of Pro * C. cx_Oracle has the following major functions.

--Basic SQL access (DML / DDL / DCL) --Execution of PL / SQL (anonymous PL / SQL, stored program) --Support for SODA (Simple Oracle Document Access, API for accessing JSON document store) --Support for XML DB (XML Type) --Support for object functions --Providing a client-side connection pool --Support for other Oracle Database functions (instance start / stop, etc.)

cx_Oracle's basic source of information

All are English based. -Official Documentation

cx_Oracle installation

Preparation

An Oracle Client is required to use cx_Oracle. There is no problem with Instant Client. There is no equivalent to Thin Driver in Java. The version of Oracle Client is compatible with 11.2 and above. On the connection destination Oracle Database Server side, the version supported by the Oracle Client you are using will be available. Before testing an application that uses cx_Oracle, make sure that you can access the Oracle Database from the machine on which you want to install cx_Oracle, such as using SQL * Plus or SQL Developer.

Installation

It can be installed using a package manager like pip or conda. The following is an installation example using pip.

pip install cx_Oracle

Sample application

sample01a.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cx_Oracle  # (1)

USERID = "admin"
PASSWORD = "FooBar"
DESTINATION = "atp1_low"
SQL = "select 12345 from dual"

connection = cx_Oracle.connect(USERID, PASSWORD, DESTINATION)  # (2)
cursor = connection.cursor()  # (3)
cursor.execute(SQL)  # (4)
for row in cursor:  # (5)
    print(row[0])  # (6)
connection.close()  # (7)

The following is an unnecessary level if you have created an application that accesses Oracle Database in other languages, but I will briefly explain each line according to the comment number in the source.

  1. You need to import cx_Oracle.
  2. Connect to the database and establish a session.
  3. Create a cursor object to handle the SQL statement.
  4. Execute the SQL statement.
  5. Since we are issuing a SELECT statement this time, we are extracting the contents of the result set.
  6. Records are tupled back.
  7. Disconnect the session.

Recommended Posts

[Introduction to cx_Oracle] Overview of cx_Oracle
[Introduction to cx_Oracle] (16th) Handling of LOB types
[Introduction to cx_Oracle] (Part 3) Basics of Table Reference
[Introduction to cx_Oracle] (5th) Handling of Japanese data
[Introduction to cx_Oracle] (Part 7) Handling of bind variables
[Introduction to cx_Oracle] (8th) cx_Oracle 8.0 release
Series: Introduction to cx_Oracle Contents
[Introduction to cx_Oracle] (Part 11) Basics of PL / SQL Execution
[Introduction to cx_Oracle] (Part 4) Fetch and scroll of result set
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction of Python
Introduction of scikit-optimize
Introduction to Tkinter 1: Introduction
Introduction of PyGMT
Introduction to PyQt
Introduction to Scrapy (2)
[Introduction to cx_Oracle] (12th) DB Exception Handling
[Introduction to cx_Oracle] (17th) Date type handling
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Introduction to discord.py (2)
[Introduction to cx_Oracle] (Part 2) Basics of connecting and disconnecting to Oracle Database
Introduction of cymel
Introduction to Scapy ① (From installation to execution of Scapy)
Introduction to discord.py
[Introduction to Data Scientists] Basics of Python ♬
Introduction of Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python 3 + Application] 19. Copy of list
From the introduction of pyethapp to the execution of contract
[Introduction to Python] Basic usage of lambda expressions
Introduction to Lightning pytorch
Introduction of ferenOS 1 (installation)
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Introduction of Virtualenv wrapper
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to PyQt4 Part 1
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to Python] Basic usage of the library matplotlib
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to cx_Oracle] (Part 13) Connection using connection pool (client side)
[Chapter 4] Introduction to Python with 100 knocks of language processing
AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4
Introduction to electronic paper modules
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Introduction to Monte Carlo Method