Access PostgreSQL using Configparser and PSYCOPG2

Source: Internet
Author: User
Tags windows x86

PSYCOPG is a PostgreSQL database interface for the Python language . Its main advantage is full support for Python DB API 2.0, as well as secure multithreaded support. It is suitable for multi-threaded database applications that create, destroy large numbers of cursors at any time, and produce large numbers of concurrent inserts and update operations. The PSYCOPG package contains Zpsycopgda, a Zope database interface. --excerpt from the Encyclopedia of Good search

Before using this, you need to download the installation package from the official website, using Psycopg2-2.5.4.win32-py2.7-pg9.3.5-release.exe (for Windows x86).

Configparser is the package that is used to read the configuration file. The format of the configuration file is as follows: the brackets "[]" are included in the section. section below is a configuration content similar to Key-value. The following format:

[SRV]
IP = 127.0.0.1

Port = 5360

Example Description:

# Introduction of PSYCOPG2 and Configparser libraries

Import PSYCOPG2

Import Configparser

#使用ConfigParser preferred to initialize the instance and read the configuration file:

initxt= "Test.ini"
Config = Configparser.configparser ()
CONFIG.READFP (open (Initxt))
ip = config.get ("SRV", "IP")
Port = Config.get ("SRV", "port")

# Connect to database test

conn = Psycopg2.connect (' host=%s port=%s dbname=test user=ent password=enttest '% (ip,port))

# Create Cursor Object

cur = conn.cursor ()

#执行sql语句 and get the results

sql = Cur.execute ("' Select mid from T_user limit 1")

MID = Cur.fetchone ()
Print Mid[0]

# 提交数据改变 conn.commit() # 关闭Cursor对象和连接对象 cur.close() conn.close()

Access PostgreSQL using Configparser and PSYCOPG2

Related Article

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.