python连接oracle
1.OCI安装
官网地址:
https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html
包地址:"Basic Light Package"
https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip
安装命令与官网一样:
cd ~
unzip instantclient-basiclite-macos.x64-19.3.0.0.0dbru.zip
mkdir ~/lib
ln -s ~/instantclient_19_3/libclntsh.dylib ~/lib/
2.安装cx_oracle
https://www.cnblogs.com/songhouhou/p/11106367.html
$ pip3 install cx_oracle
例1:
import cx_Oracle
conn = cx_Oracle.connect('user', 'password', '192.168.3.150:1521/orcl')
cursor = conn.cursor()
print("连接成功!")
cursor.close()
conn.commit()
conn.close()
例2:
# -*- coding: utf-8 -*-
importcx_Oracle
import os
os.environ['NLS_LANG'] ='SIMPLIFIED CHINESE_CHINA.UTF8'
# 设置编码,不然select出来的数据如果有中文会提示gbk无法转码
conn = cx_Oracle.connect("username/password@localhost/sid")
# 获取sid 方法,打开连接了oracle 的客户端,执行 selectinstance_namefromv$instance; 即所得
cursor = conn.cursor ()
cursor.execute ("查询语句")
row = cursor.fetchmany(numRows=3)
print(row)
cursor.close()
conn.close()
** 测试结果完美 、2020-03-20、by: muye **
网友评论