Python Connection to Oracle Tutorial in Linux Environment

Download the installation package required for Oracle client

Rpm package download address: Oracle official download address.

Select System Version.

Select Oracle version.

Download 3 rpm installation packages.

oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm
oracle-instantclient12.2-devel-12.2.0.1.0-1.i386.rpm
oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.i386.rpm

Install separately.

rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm
rpm -ivh oracle-instantclient12.2-devel-12.2.0.1.0-1.i386.rpm
rpm -ivh oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.i386.rpm
mkdir -p /usr/lib/oracle/12.2/client64/network/admin

Vim~/. bash_Profile; Or/etc/profile, etc.

export  ORACLE_HOME=/usr/lib/oracle/12.2/client64
export  TNS_ADMIN=$ORACLE_HOME/network/admin
export  LD_LIBRARY_PATH=$ORACLE_HOME/lib 
export  LANG=zh_CN.UTF-8
export  NLS_LANG=AMERICAN_AMERICA.utf8
export  PATH=$PATH:$ORACLE_HOME/bin

Source~/. bashrc
Enter the sqlplus command to test:
[sun @node01~] sqlplus.

SQL * Plus: Release 12.2.0.1.0 Production on Fri Aug 18 20:21:38 2023.

Copyright © 1982, 2016, Oracle All rights reserved.

A script for batch installation.

Python installation of cx_Oracle

Cx_Oracle is a third-party library for Python that provides the ability to interact with Oracle databases. You can use cx in Python_Oracle to connect to the Oracle database.

Online installation.

pip3 install cx_Oracle

Offline installation.

pip3 download cx_Oracle#download to the specified directory in a networked environment 
pip3 install /xxxx/cx_Oracle-8.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl#copy to another server for installation 

Writing scripts for testing.

Vim Oracle_Test. py Enter the following content:

import cx_Oracle
#connect Oracle
connection = cx_Oracle.connect(user="user name", password="password", dsn="host name : port number / service name")
#create cursor 
cursor = connection.cursor()
#implement SQL
cursor.execute("SELECT * FROM table name")
#get query results 
result = cursor.fetchall()
#print query results 
for row in result:
print(row)
#close cursor and connection 
cursor.close()
connection.close()

Batch installation script

Here is a shell script for batch executing the installation command above.

#!/bin/bash
servers="server 1 server 2 ....."
env_path="xxxxx"
for server in ${servers}
do
echo "connecting to server $server"
ssh $server"rpm -ivh /xxxxx/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm"
ssh $server "rpm -ivh /xxxxx/oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm"
ssh $server "rpm -ivh /xxxxx/oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm"
# env
ssh $server "echo 'export ORACLE_HOME=/usr/lib/oracle/12.2/client64' >> ${env_path}"
ssh $server "echo 'export TNS_ADMIN=$ORACLE_HOME/network/admin' >> ${env_path}"
ssh $server "echo 'export LD_LIBRARY_PATH=$ORACLE_HOME/lib' >> ${env_path}"
ssh $server "echo 'export LANG=zh_CN.UTF-8' >> ${env_path}"
ssh $server "echo 'export NLS_LANG=AMERICAN_AMERICA.utf8' >> ${env_path}"
ssh $server "echo 'export PATH=$PATH:$ORACLE_HOME/bin' >> ${env_path}"
# source 
ssh $server "source ${env_path}"
# cx_oracle
ssh $server "pip3 install /xxxxx/cx_Oracle-8.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl"
echo "server $server installation is complete"
done
echo "batch installation completed"