python-操作postgresql

PostgreSQL 至少有至少 3 个 Python 接口程序可
以访问 PosgreSQL: psycopg, PyPgSQL 和 PyGreSQL. 第四个, PoPy, 现在已经被废弃(2003年, 它
贡献出自己的代码, 与PygreSQL整合在一起). 这三个接口程序各有长处, 各有缺点, 根据实践结
果选择使用哪个接口是个好主意.  
 
多亏他们都支持 DB-API, 所以他们的接口基本一致, 你只需要写一个应用程序, 然后分别测
试这三个接口的性能(如果性能对你的程序很重要的化). 下面我给出这三个接口的连接代码:
psycopg
 
>>> import psycopg
>>> cxn = psycopg.connect(user='pgsql')
 
PyPgSQL
 
>>> from pyPgSQL import PgSQL
>>> cxn = PgSQL.connect(user='pgsql')
 
PyGreSQL
 
>>> import pgdb
>>> cxn = pgdb.connect(user='pgsql')

 

 

 

推荐使用http://initd.org/psycopg/

 

psycopg2 - Python-PostgreSQL Database Adapter
********************************************

psycopg2 is a PostgreSQL database adapter for the Python programming
language.  psycopg2 was written with the aim of being very small and fast,
and stable as a rock.

psycopg2 is different from the other database adapter because it was
designed for heavily multi-threaded applications that create and destroy
lots of cursors and make a conspicuous number of concurrent INSERTs or
UPDATEs. psycopg2 also provide full asycronous operations and support
for coroutine libraries.

 

1、下载,在WIN下可以直接下载它的安装包

2、启动pg数据库

3、测试代码

#!/usr/bin/env python 
import psycopg2
conn= psycopg2.connect("user=adadmin dbname=admanager") 
cur = conn.cursor() 
cur.execute('SELECT * FROM loginuser') 
rows = cur.fetchall() 
for i in rows: 
    print i 
cur.close() 
conn.commit() 
conn.close() 

4、测试结果:

F:\pro\adreamstudio\memberman>python dbconnect.py
('admin', '******************', 1)

 

5、

 

Psycopg Version Python
Version
PostgreSQL
version
built against
Release Build Debug Build
(--define PSYCOPG_DEBUG)
Current Release (2.4.2)
2.4.2
(For Python 2.4)
2.4 9.0.4 psycopg2-2.4.2.win32-py2.4-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win32-py2.4-pg9.0.4-debug.exe">psycopg2-2.4.2.win32-py2.4-pg9.0.4-debug.exe</A> -->
2.4.2
(For Python 2.5)
2.5 9.0.4 psycopg2-2.4.2.win32-py2.5-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win32-py2.5-pg9.0.4-debug.exe">psycopg2-2.4.2.win32-py2.5-pg9.0.4-debug.exe</A> -->
2.4.2
(For Python 2.6)
2.6 9.0.4 psycopg2-2.4.2.win32-py2.6-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win32-py2.6-pg9.0.4-debug.exe">psycopg2-2.4.2.win32-py2.6-pg9.0.4-debug.exe</A> -->

(For Python 2.6 amd64)
(64bit Windows)
2.6 9.0.4 psycopg2-2.4.2.win-amd64-py2.6-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win-amd64-py2.6-pg9.0.4-debug.exe">psycopg2-2.4.2.win-amd64-py2.6-pg9.0.4-debug.exe</A> -->
2.4.2
(For Python 2.7)
2.7 9.0.4 psycopg2-2.4.2.win32-py2.7-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win32-py2.7-pg9.0.4-debug.exe">psycopg2-2.4.2.win32-py2.7-pg9.0.4-debug.exe</A> -->

(For Python 2.7 amd64)
(64bit Windows)
2.7 9.0.4 psycopg2-2.4.2.win-amd64-py2.7-pg9.0.4-release.exe <!-- <A href="http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.4.2.win-amd64-py2.7-pg9.0.4-debug.exe">psycopg2-2.4.2.win-amd64-py2.7-pg9.0.4-debug.exe</A> -->
2.4.2
(For Python 3.2)
3.2 9.0.4 psycopg2-2.4.2.win32-py3.2-pg9.0.4-release.exe  

(For Python 3.2 amd64)
(64bit Windows)
3.2 9.0.4 psycopg2-2.4.2.win-amd64-py3.2-pg9.0.4-release.exe

你可能感兴趣的:(PostgreSQL)