Anyone know a good Pygresql Tutorial for Interfacing betweenPython &Postgresql

C

Chuck Amadi

Hi all

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

Cheers

Chuck
 
J

John fabiani

Peter said:
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
 
P

Peter Maas

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top