Oracle for python -- continued, pythonoracle --
Next
Iii. Connecting to the oracle configuration file
To increase program portability
Db = cx_Oracle.connect ('oss _ cpc/bss_cpc@192.168.128.49/orcl ')
To:
Db = cx_Oracle (connStr), the value of connStr is read from the configuration file.
You can use built-in modules to read configuration files.
Import configparser --> python3 import ConfigParser --> python2
My configuration file content:
[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 ()
# Read files (if the file and python are not in the same directory, the path must be included)
Cf. read ("file ")
# Read the section in the file as oracle and assign it to the variable
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 ")
Some connections do not need ports, so the judgment is added to the difference.
If port = "":
ConnStr = user + '/' + _ pass + '@' + host + '/' + inst
Else:
ConnStr = user + '/' + _ pass + '@' + host + ':' + 'Port' + '/' + inst
Db = cx_Oracle (connStr)
Read the configuration file and connect it to oracle. The rest is simple. If you have any questions or better comments, you can leave a message. We can learn and make progress together. In addition, this year, python has surpassed java to become the world's most popular programming language. Come on !!!
Attach configparser basic operations
1. Read the configuration file.
-Read (filename) directly reads the INI File Content
-Sections () obtains all sections and returns them as a list.
-Options (section): obtain all options of this section.
-Items (section) obtains all key-value pairs of this section.
-Get (section, option): get the option value in section. The return value is of the string type.
-Getint (section, option) gets the option value in the section, returns the int type, and the corresponding getboolean () and getfloat () functions.
2. Basic write configuration file
-Add_section (section): Add a new section.
-Set (section, option, value) to set the option in section. You need to call write to write the content to the configuration file.