[PYTHON] [Introduction to cx_Oracle] (Part 2) Basics of connecting and disconnecting to Oracle Database

Verification environment

--Using Oracle Cloud

DB connection basics

It is not limited to cx_Oracle, but to connect to Oracle Database, you need the user name of the DB to be connected, the password of that user, and the connection destination information (either TNS connector or EZCONNECT). These need to be prepared in advance. In cx_Oracle, you can connect to the DB by specifying these in the argument of the method called connect (). There is also an alias called connection (), which you can use.

For example, if the user name is "scott", the password is "Tiger", and the connection destination information is "db1" connection = cx_Oracle.connect("scott", "Tiger", "db1") Connect to the DB by specifying the user name in the first argument (argument name user), the password (argument name password) in the second argument, and the connection destination information in the third argument (argument name dsn). I can do it. Even if the connection destination information is in EZCONNECT format such as "hostname1: 1521 / db1.oracle.com" connection = cx_Oracle.connect("scott", "Tiger", "hostname1:1521/db1.oracle.com") And just specify it in the same way. The dsn argument is not required for local connections using the environment variable ORACLE_SID.

This method returns a Connection object. After that, perform operations such as issuing SQL to the acquired Connection object (variable connection in the above example).

DB disconnection basics

Call the close () method of the Connection object. connection.close() The following is a sample application that connects to a DB, displays the version information of the connection destination DB (second line from the bottom), and disconnects.

sample02a.py


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

USERID = "admin"
PASSWORD = "FooBar"
DESTINATION = "atp1_low"

connection = cx_Oracle.connect(USERID, PASSWORD, DESTINATION)
print(connection.version)
connection.close()

A more secure connection using the with syntax

Closing open resources is a programming rule. However, in reality, it's easy to forget about closing. Python provides a with syntax to ensure that open resources are closed. And cx_Oracle supports the with syntax. By coding as follows, it is possible to securely close the connection.

sample02b.py


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

USERID = "admin"
PASSWORD = "FooBar"
DESTINATION = "atp1_low"

with cx_Oracle.connect(USERID, PASSWORD, DESTINATION) as connection:
        print(connection.version)

change history

Recommended Posts

[Introduction to cx_Oracle] (Part 2) Basics of connecting and disconnecting to Oracle Database
[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 cx_Oracle] (Part 7) Handling of bind variables
[Introduction to cx_Oracle] Overview of cx_Oracle
[Python3] Connection to Oracle Database and SQL execution [cx_Oracle]
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Data Scientists] Basics of Python ♬
[Introduction to cx_Oracle] (16th) Handling of LOB types
[Introduction to cx_Oracle] (5th) Handling of Japanese data
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
Introduction to PyQt4 Part 1
Speeding up when connecting from cx_Oracle to Autonomous Database
[Introduction to cx_Oracle] (Part 13) Connection using connection pool (client side)
[Introduction to cx_Oracle] (10th) Update DML and DDL / DCL statements
[Introduction to Scipy] Calculation of Lorenz curve and Gini coefficient ♬
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
Introduction of DataLiner ver.1.3 and how to use Union Append
Introduction to Anomaly Detection 1 Basics
[Introduction to cx_Oracle] (8th) cx_Oracle 8.0 release
Introduction to Ansible Part ③'Inventory'
Series: Introduction to cx_Oracle Contents
Introduction and tips of mlflow.Tracking
Basics of Python × GIS (Part 1)
Introduction to Ansible Part ④'Variable'
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Scipy
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Pandas
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Matplotlib
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to pytorch-lightning] Autoencoder of MNIST and Cifar10 made from scratch ♬
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Introduction to TensorFlow-Summary of four arithmetic operations and basic mathematical functions
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Basics of Python x GIS (Part 3)
Introduction and Implementation of JoCoR-Loss (CVPR2020)
Introduction to Ansible Part 2'Basic Grammar'
[Introduction to Python3 Day 1] Programming and Python
Introduction and implementation of activation function
Introduction to Python Hands On Part 1
Basics of Python x GIS (Part 2)
Introduction to Ansible Part 1'Hello World !!'
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use the graph drawing library ♬ Environment construction