@创建于: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 userenv('language') nls_lang from dual") #显示数据库的字符集
cursor.execute("select * from R_DATA_MP") # 显示数据库的字符集
result = cursor.fetchmany(10)
print(result)
cursor.close()
connection.close()
3、官网推荐
import cx_Oracle
userpwd = ". . ." # Obtain password string from a user prompt or environment variable
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")