Connecting to a postgresql DB?
Tim Roberts
timr at probo.com
Sat Sep 11 01:57:25 EDT 2004
Lance Hoffmeyer <lance-news at augustmail.com> 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.
>
>Can someone show me an example of how to do this?
Postgres support is not built-in to Python. You'll need to install one of
the Postgres packages, like pgdb or pypgsql or psycopg. They are all
compliant with the Python DBAPI spec, so the usage is pretty similar:
import psycopg
db = psycopg.connect("dbname=db user=USER password=PW")
cur = db.cursor()
cur.execute("SELECT first_name, last_name, birthday FROM contacts")
for first,last,bday in cur.fetchall():
print "%s %s has birthday %s" % (first, last, bday)
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list