Python连结Oracle数据库的环境搭建和基本代码
原创
©著作权归作者所有:来自51CTO博客作者小龙在山东的原创作品,请联系作者获取转载授权,否则将追究法律责任
本机环境
Windows Server 2019
Python 3.8.5
下载oracle数据库instantclient
https://www.oracle.com/technetwork/topics/winx64soft-089540.html
安装cx-Oracle
从https://pypi.org/project/cx-Oracle/#files下载对应版本的cx_Oracle-8.1.0-cp38-cp38-win_amd64.whl。
用pip安装:
pip install cx_Oracle-8.1.0-cp38-cp38-win_amd64.whl
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()