Connecting to a postgresql DB?

  • Thread starter Lance Hoffmeyer
  • Start date
L

Lance Hoffmeyer

Hello,

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?

Lance
 
D

Dave Cook

Lance Hoffmeyer said:
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.

This assumes you have the standard postgresql-python package. You may also
want to try psycopg or pyPgSQL.
import pgdb
conn = pgdb.connect(database='DB', user='USER', password='PW')
cursor = conn.cursor()
cursor.execute("SELECT first_name, last_name, birthday FROM contacts")
colnames = [t[0] for t in cursor.description]
rows = cursor.fetchall()
data = [dict(zip(colnames, row)) for row in rows]
print data

Dave Cook
 
P

Peter Maas

Lance said:
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/eGenix-mx-Extensions.html
- pypgSQL:
http://sourceforge.net/project/showfiles.php?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(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
 
T

Tim Roberts

Lance Hoffmeyer said:
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)
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top