zoukankan      html  css  js  c++  java
  • 操作oracle 数据库 python

    #  需要修改oracle的配置数据库
    import cx_Oracle
    from Common.dir_config import caps_dir
    import yaml
    class DoSql:
        def do_orcal(self,query_sql,state='all'):
            # 1打开yaml文件  放置的是数据库的地址和密码用户名
            fs = open(caps_dir+"/oracle.yaml")
            # 2转换成python对象
            db_config = yaml.load(fs, Loader=yaml.FullLoader)
            conn=cx_Oracle.connect(db_config['db_config'])
            #游标cursor
            cursor=conn.cursor()
            #写sql语句--是字符串
            #执行语句
            cursor.execute(query_sql)
            #获取结果,打印结果
            if state==1:
                res=cursor.fetchone()#返回的是元祖  针对一条数据
            else:
                res=cursor.fetchall()#列表,针对多行数据,列表嵌套元祖
            #关闭游标
            cursor.close()
            #关闭连接
            conn.close()
            return res
    if __name__ == '__main__':
    
        query_sql='select * from DEPT t '
        print(DoSql().do_orcal(query_sql,1)[1])   #取值

    caps_dir.py  放oracle数据库信息的配置文件

    import os
    caps_dir = os.path.join(base_dir,"Desired_Caps")

    oracle数据库的地址和密码放到配置文件(起名叫oracle.yaml):db_config: scott/tiger@localhost:1521/orcl    这个是安装上oracle 自身带的数据库  可以练习

    要快乐,要有梦想,要自信
  • 相关阅读:
    设计模式 创建型 单例模式
    设计模式 创建型 抽象工厂模式
    设计模式六大原则
    设计模式 创建型 工厂方法模式
    设计模式 创建型 简单工厂模式
    Junit TDD
    覆盖索引
    多列索引 单列索引
    JDBC PreparedStatement Statement
    常用·SQL
  • 原文地址:https://www.cnblogs.com/zhangjie198212/p/14605428.html
Copyright © 2011-2022 走看看