zoukankan      html  css  js  c++  java
  • python操作Oracle--cx_Oracle模块(第三方)

    第一part:cx_Oracle模块的安装


     第一种安装方式通过命令行: pip install  cx_Oracle

    第二种方式通过pycharm--project--settings--project interpreter中搜索cx_Oracle进行安装


     第二part:安装Oracle版本对应的客户端,并配置到path变量中


    python连接Oracle数据库前, 必须要安装Oracle版本对应的客户端,否则python对Oracle操作时会抛异常。

    下载官网:https://oracle.github.io/odpi/doc/installation.html#windows,必须与Oracle版本对应的client

    1.下载成功之后,解压到一个新的文件夹中:

     2.并将其配置到path变量中:

     3.重启pycharm即可。


     第三part:python操作cx_Oracle模块---连接Oracle的方式


    第一种方式:普通用户

    如果端口号默认1521可以省略
    #导入模块
    import cx_Oracle
    get_conn1 = cx_Oracle.connect('scott','tiger','localhost:1521/orcl')


    第二种方式:普通用户
    #导入模块
    import cx_Oracle
    get_conn2 = cx_Oracle.connect('scott/tiger@localhost:1521/orcl')


    第三种方式:系统管理员
    #导入模块
    import cx_Oracle
    get_conn3 = cx_Oracle.connect('sys/123456@localhost:1521/orcl',mode=cx_Oracle.SYSDBA)


    第四种方式:dsn_tns
    #导入模块
    import cx_Oracle
    dsn_tns = cx_Oracle.makedsn('localhost', 1521, 'orcl')
    get_conn4 = cx_Oracle.connect('scott', 'tiger', dsn_tns)


    第四part:python对Oracle数据库的新增,修改,删除,查询以及回滚数据


     python对Oracle数据库的新增,修改,删除,查询以及回滚数据与python对mysql数据库的操作是一致的,详细操作可见上一篇文章:https://www.cnblogs.com/smilecindy/p/13777779.html

    在此处只简单的介绍一下查询操作:

    #1:导入cx_Oracle模块
    import cx_Oracle
    #2,连接orcle数据库,使用connect函数,其中包含用户名,密码,数据库服务地址:端口号/orcl
    get_connect=cx_Oracle.connect('scott','123456','localhost:1521/orcl')
    #3:创建游标,用于获取结果集
    get_cursor=get_connect.cursor()
    #4:定义一条sql语句
    str_sql="select * from emp where empno='7369'"
    # 5:使用游标进行执行sql
    get_cursor.execute(str_sql)
    #6.获取结果值,fetchall表示查询所有的记录
    result=get_cursor.fetchall()
    print(result)
    #7:关闭游标连接
    get_cursor.close()
    #8:关闭数据库连接
    get_connect.close()

    执行之后,结果如下:

  • 相关阅读:
    【模板】可持久化线段树
    【模板】可持久化权值线段树(主席树)
    BZOJ 2456 Mode
    【模板】可持久化Treap
    BZOJ 1452 Count 【模板】二维树状数组
    高级线程之线程池
    STL优先队列重载
    单链表及简单应用
    2017 计蒜之道 初赛 第一场 A 阿里的新游戏
    2017 计蒜之道 初赛 第一场 B阿里天池的新任务(简单)
  • 原文地址:https://www.cnblogs.com/smilecindy/p/13777998.html
Copyright © 2011-2022 走看看