本机环境

Windows Server 2019

Python 3.8.5

Python连结Oracle数据库的环境搭建和基本代码_oracle

Python连结Oracle数据库的环境搭建和基本代码_python_02

下载oracle数据库instantclient

​https://www.oracle.com/technetwork/topics/winx64soft-089540.html​

Python连结Oracle数据库的环境搭建和基本代码_python_03

安装cx-Oracle

从​​https://pypi.org/project/cx-Oracle/#files​​下载对应版本的cx_Oracle-8.1.0-cp38-cp38-win_amd64.whl。

Python连结Oracle数据库的环境搭建和基本代码_oracle_04

用pip安装:

pip install cx_Oracle-8.1.0-cp38-cp38-win_amd64.whl

Python连结Oracle数据库的环境搭建和基本代码_oracle_05

Python连结Oracle

def get_news():
connection = cx.connect("USERNAME/PASSWORD@IP:PORT/SID")
cursor = connection.cursor()
sql = "SELECT * FROM XY_ARTICLE a JOIN XY_COLUMN b ON a.A_COLUMNALL=b.SYS_DOCUMENTID WHERE ROWNUM<5 "
res = cursor.execute(sql)
all = cursor.fetchall()

for x in one:
print(x)

cursor.close()
connection.close()