By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Anyone know a good Pygresql Tutorial for Interfacing betweenPython &Postgresql

P: n/a
Hi all

Anyone know a good Pygresql Tutorial for Interfacing between Python &
Postgresql .

Cheers

Chuck
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chuck Amadi schrieb:
Anyone know a good Pygresql Tutorial for Interfacing between Python &
Postgresql .


Tried Google and found

http://www.pygresql.org/README.txt
Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Jul 18 '05 #2

P: n/a
Peter Maas wrote:
Chuck Amadi schrieb:
Anyone know a good Pygresql Tutorial for Interfacing between Python &
Postgresql .

Tried Google and found

http://www.pygresql.org/README.txt
Mit freundlichen Gruessen,

Peter Maas

I'm a newbie so take what I have to say with a grain of salt. The
problem for me was not how to make a connection to postgres but how to
use the data returned. Maybe I'm missing something but almost every
sample I could find did not work as expected. The first issue was each
example (ones found via google and in O'Reilly books) show that a tuple
is returned. At least for my postgres driver (module) the return type
is a list. The next issue is how to loop through the data. Also the
examples use fetchone() as an example. But even they did not work
because of the 'tuple' issue and most of the time I needed
fetchmany(100) or fetchall(). I still have not resolved most of the
issues but I'm still learning.
John
Jul 18 '05 #3

P: n/a
John fabiani schrieb:
I'm a newbie so take what I have to say with a grain of salt. The
problem for me was not how to make a connection to postgres but how to
use the data returned.


Here is a tested example code. It relies on pyPgSQL but PyGreSQL
should be quite similar especially the data access:

-----------------------------------------------------------------
#!/usr/bin/env python
# -*- coding: latin-1 -*-

"""
Example code how to read data from a PostgreSQL database. You
need the pyPgSQL module which is DB-API 2.0 compliant so that
the calls are not database dependent except of connection URL
and some SQL capabilities.
"""

# PostgreSQL interface module
from pyPgSQL import PgSQL

if __name__ == '__main__':
# open connection
con = PgSQL.connect(None, "aUser", "aPasswd", "aHost", "aDatabase")

# create cursor
c_adr = con.cursor()

# let cursor execute an SQL command
c_adr.execute("SELECT * FROM address")

# fetch a result set
r_adr = c_adr.fetchmany(10)

# The result set is a list of records.
print r_adr[0]

# Each record is a dictionary like object with field names as keys.
print r_adr[0].keys()

# The field values are the dictionary values.
print r_adr[0]["firstname"]

# print all records
for record in r_adr:
print record
-----------------------------------------------------------------
Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Jul 18 '05 #4

P: n/a
Peter Maas wrote:
Chuck Amadi schrieb:
Anyone know a good Pygresql Tutorial for Interfacing between Python &
Postgresql .

Tried Google and found

http://www.pygresql.org/README.txt


This describes the PyGreSQL proprietory interface. It would IMO be a
much better idea to use a DB-API compliant interface, like pyPgSQL or
psycopg offer. Or PyGreSQL through the pgdb module included.

-- Gerhard

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.