@创建于:2022.04.29 @修改于:2022.04.29
先安装cx_Oracle模块,然后进行读取。
1、安装包
pip install cx_Oracle
conda install cx_Oracle
2、读取代码段
def connct_oracle(self):
'''
从公司紫光云读取数据。
:return:
'''
print("cx_Oracle.version:", cx_Oracle.version)
host = "172.XX.X.X"
port = "1521"
sid = 'ORCLCDB'
service_name = 'ORCLCDB.localdomain'
use_sid = True
if use_sid:
dsn = cx_Oracle.makedsn(host, port, sid=sid)
else:
dsn = cx_Oracle.makedsn(host, port, service_name=service_name)
print(dsn)
connection = cx_Oracle.connect(user="username", password="password", dsn=dsn)
cursor = connection.cursor()
cursor.execute("select * from R_DATA_MP")
result = cursor.fetchmany(10)
print(result)
cursor.close()
connection.close()
3、官网推荐
import cx_Oracle
userpwd = ". . ."
connection = cx_Oracle.connect(user="hr", password=userpwd,
dsn="dbhost.example.com/orclpdb1",
encoding="UTF-8")
dsn = cx_Oracle.makedsn("dbhost.example.com", 1521, service_name="orclpdb1")
connection = cx_Oracle.connect(user="hr", password=userpwd, dsn=dsn,
encoding="UTF-8")
Connecting to Oracle Database
4、参考资料
Connecting to Oracle Database
Python cx_Oracle.makedsn方法代码示例
|