1. Oracle client installation
1. System description
Operating system: CentOS 5.11
1. 2. Download Software
You need to install the oracle client program which contains the following three programs
1) oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
2) oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
3) oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
You can download other operating systems from here:
Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
1.3. Install software
Run the following installation command:
Rpm-ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
Rpm-ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
Rpm-ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
2. cx_Oracle plug-in installation
2.1. Software version
Python version: 2.7.10
2.2. Download the plug-in
Download Address: https://pypi.python.org/pypi/cx_Oracle/5.2
Download cx_Oracle-5.2.tar.gz (md5) here)
2.3. Compile and install
Run the following command:
$ Cx_Oracle-5.2 cd
$ Python27 setup. py build
$ Python27 setup. py install
3. Connection test
3.1. Python script
#! /Usr/bin/env python27
# Coding: utf8
Import cx_Oracle as oracle
User = "username"
Passwd = "password"
Host = "127.0.0.1"
Port = "1501"
Sid = "ORACLE-Service-Name"
Dsn = oracle. makedsn (host, port, service_name = sid)
Conn = oracle. connect (user, passwd, dsn)
Cursor = conn. cursor ()
SQL = "SELECT PERSON_ACCOUNT, PERSON_NAME FROM V_INFO_PERSON WHERE ROWNUM <= 10"
Cursor.exe cute (SQL)
Result = cursor. fetchall ()
Print result
3.2. Execute the script
$ Python27 test. py
3.3. Error message
3.3.1 ORA-21561
The error message returned when the script is executed is as follows:
Cx_Oracle.DatabaseError: ORA-21561: OID generation failed
This is because the host name of the local computer is inconsistent with that of/etc/hosts. Because the hostname of our local server is dbmonitor, therefore, modify the data of row 127.0.0.1 in/etc/hosts as follows:
127.0.0.1 localhost dbmonitor
3.3.2 ORA-12505
The error message returned when the script is executed is as follows:
Cx_Oracle.DatabaseError: ORA-12505: TNS: listener does not currently know of SID given in connect descriptor
This is because an error is reported when dsn = oracle. makedsn (host, port, sid) is executed. The TNS parameter for connecting to the ORACLE database is set as follows:
TNS:
V-SAP =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1501 ))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = SAP.V-CLUSTER.COM)
)
)
The syntax of makedsn in cx_Oracle is as follows:
Cx_Oracle.makedsn (host, port, sid [, service_name])
Because we do not know the sid in the connection parameter, we need to change this row to service_name and modify the script for this row as follows:
Dsn = oracle. makedsn (host, port, service_name = sid)