Oracle for python -- continued, pythonoracle --

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.