栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 读取 Oracle 的数据

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 读取 Oracle 的数据

@创建于:2022.04.29
@修改于:2022.04.29

文章目录
    • 1、安装包
    • 2、读取代码段
    • 3、官网推荐
    • 4、参考资料

先安装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方法代码示例

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844581.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号