Connecting to a postgresql DB?
Dave Cook
davecook at nowhere.net
Thu Sep 9 00:30:57 EDT 2004
In article <pan.2004.09.08.21.54.22.327805 at augustmail.com>, Lance Hoffmeyer
wrote:
> I am trying to learn some basics of python. One of the things
> I want to do is write a script to access a postgresql database
> DB as user USER with password PW and
>
> SELECT first_name, last_name, birthday FROM contacts
>
> print to the screen and then disconnect.
This assumes you have the standard postgresql-python package. You may also
want to try psycopg or pyPgSQL.
>>> import pgdb
>>> conn = pgdb.connect(database='DB', user='USER', password='PW')
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT first_name, last_name, birthday FROM contacts")
>>> colnames = [t[0] for t in cursor.description]
>>> rows = cursor.fetchall()
>>> data = [dict(zip(colnames, row)) for row in rows]
>>> print data
Dave Cook
More information about the Python-list
mailing list