Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / psycopg2 / examples / dict.py
1 # dict.py - using DictCUrsor/DictRow
2 #
3 # Copyright (C) 2005 Federico Di Gregorio  <fog@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any later
8 # version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14
15 ## put in DSN your DSN string
16
17 DSN = 'dbname=test'
18
19 ## don't modify anything below this line (except for experimenting)
20
21 import sys
22 import psycopg2
23 import psycopg2.extras
24
25 if len(sys.argv) > 1:
26     DSN = sys.argv[1]
27
28 print "Opening connection using dsn:", DSN
29 conn = psycopg2.connect(DSN)
30 print "Encoding for this connection is", conn.encoding
31
32     
33 curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
34 curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot")
35
36 data = curs.fetchone()
37 print "Some data accessed both as tuple and dict:"
38 print " ", data['foo'], data['bar'], data['zot']
39 print " ", data[0], data[1], data[2]
40
41 # execute another query and demostrate we can still access the row
42 curs.execute("SELECT 2 AS foo")
43 print "Some more data accessed both as tuple and dict:"
44 print " ", data['foo'], data['bar'], data['zot']
45 print " ", data[0], data[1], data[2]