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