zoukankan      html  css  js  c++  java
  • python接口自动化测试框架实现之操作oracle数据库

    python操作oracle数据库需要使用到cx-oracle库。

    安装:pip install cx-oracle

    python连接oracle数据库分以下步骤:

    1、与oracle建立连接;

    2、获取游标;

    3、执行sql语句;

    4、fetch查询结果或commit修改结果;

    5、关闭游标;

    6、关闭oracle连接。

    完整示例如下:

    # -*- coding:utf-8 -*-
    
    import cx_Oracle as oracle
    from readConfig import get_config_values
    from utils.resutltodict import dict_fetchall
    
    
    def execute_oracle_sql_query(sql, params):
        """
        执行oracle sql查询语句
        :param sql: sql语句,变量使用 :var或者:1,:2表示
        :param params: 变量值,传入元祖
        :return: queryset
        """
        dsn_tns = oracle.makedsn(get_config_values('oracle', 'host'), get_config_values('oracle', 'port'),
                                 get_config_values('oracle', 'sid'))
        print(dsn_tns)
        conn = oracle.connect(user=get_config_values('oracle', 'user'), password=get_config_values('oracle', 'password'),
                              dsn=dsn_tns)
        cursor = conn.cursor()
        cursor.execute(sql, params)
        qryset = dict_fetchall(cursor)
        cursor.close()
        conn.close()
        return qryset
    
    if __name__ == '__main__':
        sql = """select t.*,rowid from ch_info_dictitem t where t.groupid=:1"""
        params = ('WorkFlowCategory', )
        print(execute_oracle_sql_query(sql=sql, params=params))

    cx_oracle的详细使用见官文:

    https://oracle.github.io/python-cx_Oracle/

  • 相关阅读:
    Apache安装与属性配置
    Web服务及http协议
    转-httpd 2.4.4 + mysql-5.5.28 + php-5.4.13编译安装过程
    LAMP理论整理
    Rhel6-csync配置文档
    转载Eclipse中Maven WEB工程tomcat项目添加调试
    转载--eclipse git插件安装
    l连接远程桌面
    Aphache VFS
    JMS-activeMQ
  • 原文地址:https://www.cnblogs.com/ianduin/p/8556674.html
Copyright © 2011-2022 走看看