python 读取 Oracle 的数据

@创建于: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")

Connecting to Oracle Database

4、参考资料

Connecting to Oracle Database

Python cx_Oracle.makedsn方法代码示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值