7

哪个是python中连接postgresql的最佳驱动程序?

有几种可能性,http://wiki.postgresql.org/wiki/Python但我不知道哪个是最佳选择

任何想法?

4

2 回答 2

14

psycopg2 是每个人都与 CPython 一起使用的。不过,对于 PyPy,你会想看看纯 Python 的。

于 2012-05-05T19:49:03.333 回答
11

I would recommend sqlalchemy - it offers great flexibility and has a sophisticated inteface.

Futhermore it's not bound to postgresql alone.

Shameless c&p from the tutorial:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# an Engine, which the Session will use for connection
# resources
some_engine = create_engine('postgresql://scott:tiger@localhost/')

# create a configured "Session" class
Session = sessionmaker(bind=some_engine)

# create a Session
session = Session()

# work with sess
myobject = MyObject('foo', 'bar')
session.add(myobject)
session.commit()

Clarifications due to comments (update):

sqlalchemy itself is not a driver, but a so called Object Relational Mapper. It does provide and include it's own drivers, which in the postgresql-case is libpq, which itself is wrapped in psycopg2.

Because the OP emphasized he wanted the "best driver" to "connect to postgresql" i pointed sqlalchemy out, even if it might be a false answer terminology-wise, but intention-wise i felt it to be the more useful one.

And even if i do not like the "hair-splitting" dance, i still ended up doing it nonetheless, due to the pressure felt coming from the comments to my answer.

I apologize for any irritations caused by my slander.

于 2012-05-05T20:20:30.510 回答