Lance Hoffmeyer schrieb:
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.
URLs for required modules:
- mxDateTime
http://www.egenix.com/files/python/e...xtensions.html
- pypgSQL:
http://sourceforge.net/project/showf...group_id=16528
Example code:
#!/usr/bin/env python
# -*- coding: latin-1 -*-
"""
Example code for reading data from a PostgreSQL database. This
code requires the modules mxDateTime and pyPgSQL (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(N one, "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, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
-------------------------------------------------------------------