Centos7下用Python3连接Oracle

  1. 需要的包
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

  1. 安装:
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'  --测试导入模块

如果没有报错,说明安装成功。

  1. 配置环境变量
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()

执行:

[root@ostjdbatest02 python-package]# python3 test_oracle.python
[(87450,)]

你可能感兴趣的:(Centos7下用Python3连接Oracle)