Saturday, August 08, 2009

Using Named Columns in Python

% sudo apt-get install python-psycopg2
% python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("host='localhost' dbname='satoshiabe' user='satoshiabe' password='satoshiabe'");
>>> cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
>>> cur.execute("""SELECT * FROM table01""")
>>> rows = cur.fetchall()
>>> for row in rows:
...        print row['user_name']

No comments: