1、先安装python cx_Oracle模块
pip3 install cx_Oracle
2、下载oracle连接客户端,到下面地址下载对应版本的客户端
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
使用下面命令安装rpm
rpm -ivh XXX.rpm
3、配置环境变量
如未配置环境变量,会提示“cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library”这种错误。添加方法:
vim /etc/profile
添加
export ORACLE_HOME=/usr/lib/oracle/12.2/client64/lib export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
编辑完成后执行:
source /etc/profile
可执行env命令查看是否添加成功
4、写python测试
import cx_Oracle conn = cx_Oracle.connect('root', '111111', '127.0.0.1:1521/orcl') cursor = conn.cursor() search_sql="select * from m_sms_data where rownum<10" result=cursor.execute(search_sql) data = cursor.fetchall() print(data) cursor.close() conn.close()