Thread: PyPgSQL help

PyPgSQL help

From
Bob Jarman
Date:
Hello,

Off-topic re Python.  I'm trying to connect to Postgres 7.4.2 using
python.  The only doc I have is the Geschwinde  and Schoenig book, which
is apparently dated as 'import pg' throws an error (code snippet on page
405).  Commenting the 'import' parses correctly but does not establish a
connection - I know the dbs, urls etc are valid since I can reach
everything through the JDBC using DB VIs and Xpg.  I downloaded and
installed the PyPgSQL rpm (SuSE 9.1 Pro) - all distro modules for
PostgreSQL and Pyhton are installed along with Apache2.

Can someone point me to some docs re python & postgres?

reply via e-mail is fine....

thx

Bob


Re: PyPgSQL help

From
Oliver Fromme
Date:
Bob Jarman wrote:
 > Off-topic re Python.  I'm trying to connect to Postgres 7.4.2 using
 > python.

I'm using PyGreSQL:  http://www.druid.net/pygresql/
You simply "import pgdb" and get a Python DB-API 2.0-
compliant interface.  Here's a short excerpt from one
of my Python programs (simplified for brevity):

    import sys
        import pgdb

        try:
                db = pgdb.connect(host = "foo.bar", database = "baz",
                                  user = "chuck", password = "xxxxxxxx")
        except pgdb.Error, detail:
                print "Cannot open connection to database!"
                print detail
                sys.exit (1)

        try:
                cursor = db.cursor()
        except pgdb.Error, detail:
                print "Cannot acquire cursor for database!"
                print detail
                sys.exit (1)

        try:
                cursor.execute ("SELECT * FROM moonlight")
                result = cursor.fetchall()
        except pgdb.DatabaseError, detail:
                print "SQL query failed!"
                print detail
                sys.exit (1)

    db.close()

The full Python DB-API 2.0 documentation can be found in
PEP 249:  http://www.python.org/peps/pep-0249.html

Best regards
   Oliver

--
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

Python is executable pseudocode.  Perl is executable line noise.