zoukankan      html  css  js  c++  java
  • python之oracle--续

    接上篇

    三、连接oracle之配置文件

    为了增加程序的可移植性,将 

    db = cx_Oracle.connect('bss_cpc/bss_cpc@192.168.128.49/orcl')

    修改为:

    db = cx_Oracle(connStr),connStr值从配置文件中读取。

    读取配置文件可以使用内置模块

    import configparser     -->python3       import ConfigParser   -->python2

    本人的配置文件内容:

    [oracle]
    db_user = bss_cpc
    db_pass = bss_cpc
    db_host = 192.168.128.49
    db_port =
    db_space =
    db_inst = orcl

    cf = configparser.ConfigParser()  

    #读取文件 (如果文件和python不在一个目录下,需要包含路径)

    cf.read("文件")    

    #将文件下section为oracle的内容读取并赋值给变量

    user = cf.get("oracle","db_user")    

    _pass = cf.get("oracle","db_pass")

    host = cf.get("oracle","db_host")

    port = cf.get("oracle","db_port")

    inst = cf.get("oracle","db_inst")

    有的连接可以不加端口,为了区别加了判断

    if port == "":

      connStr = user + '/' +_pass + '@' + host + '/' +inst

    else:

      connStr = user + '/' +_pass + '@' + host + ':' + 'port' + '/' +inst

    db = cx_Oracle(connStr)

    到此读取配置文件连oracle搞定,剩下的都是简单的执行了。如有疑问或是更好的意见可以留言,我们可以一起学习,共同进步,另外今年python已超越java成为全球最受欢迎的编程语言了,加油!!!

    附configparser基本操作

    1.基本的读取配置文件

    -read(filename) 直接读取ini文件内容

    -sections() 得到所有的section,并以列表的形式返回

    -options(section) 得到该section的所有option

    -items(section) 得到该section的所有键值对

    -get(section,option) 得到section中option的值,返回为string类型

    -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

    2.基本的写入配置文件

    -add_section(section) 添加一个新的section

    -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件

  • 相关阅读:
    vue使用video.js解决m3u8视频播放格式
    小程序索引列表排序
    小程序实现图片上传,预览以及图片base64位处理
    小程序实现综合排序页面布局
    前端学习 html
    Linux Yum仓库介绍及服务端及客户端配置
    Saltstack 常用的模块及API
    KVM 克隆虚拟机
    KVM 虚拟机基本管理及常用命令
    Saltstack 操作目标,正则匹配,及组管理
  • 原文地址:https://www.cnblogs.com/hqd2008/p/7665859.html
Copyright © 2011-2022 走看看