- 需要的包
python3 --不多介紹了
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm,
下載地址:
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
cx_Oracle-7.1.2.tar
- 安裝:
rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
tar -xvf cx_Oracle-7.1.2.tar
cd cx_Oracle-7.1.2/
python3 setup.py install
python3 -c 'import cx_Oracle' --測試導(dǎo)入模塊
如果沒有報錯锋叨,說明安裝成功。
- 配置環(huán)境變量
vi /root/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export ORACLE_HOME=/usr/lib/oracle/11.2/client64/lib
export LD_LIBRARY_PATH=$LD_LIBARARY_PATH:$ORACLE_HOME
export PATH
source /root/.bash_profile
寫腳本測試:
[root@ostjdbatest02 python-package]# more test_oracle.python
#/usr/bin/env python3
# -*- coding:utf-8 -*-
#Author:ld
import cx_Oracle
def Oracle_Lock():
conn = cx_Oracle.connect('system', 'xxxx', 'xxx.xxx.xxx.xxx:1521/orcl')
cursor = conn.cursor()
#sql = 'SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s WHERE l.object_id = o.object_id AND l.session_id = s.sid'
sql = 'select count(*) from dba_objects'
result = cursor.execute(sql)
all_data = cursor.fetchall()
cursor.close()
conn.commit()
conn.close()
print(all_data)
return all_data
if __name__ == '__main__':
Oracle_Lock()
執(zhí)行:
[root@ostjdbatest02 python-package]# python3 test_oracle.python
[(87450,)]