Wie kann es sein, dass als Ergbnis hier eine Liste rauskommt und kein Dictionnary?
Code: Alles auswählen
# -*- coding: iso-8859-1 -*-
# load the adapter
import psycopg2
# load the psycopg extras module
import psycopg2.extras
conn = psycopg2.connect("dbname=test user=testuser password=123456")
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("""
SELECT * from contacts
""")
data = cur.fetchall()
print type(data)
conn.commit()
Hab dieses Skript gefunden in dem es zu funktionieren scheint.
http://www.devx.com/opensource/Article/29071/0/page/3
Code: Alles auswählen
#/usr/bin/python2.4
#
#
# load the adapter
import psycopg2
# load the psycopg extras module
import psycopg2.extras
# Try to connect
try:
conn=psycopg2.connect("dbname='foo' user='dbuser' password='mypass'")
except:
print "I am unable to connect to the database."
# If we are accessing the rows via column name instead of position we
# need to add the arguments to conn.cursor.
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
try:
cur.execute("""SELECT * from bar""")
except:
print "I can't SELECT from bar"
#
# Note that below we are accessing the row via the column name.
rows = cur.fetchall()
for row in rows:
print " ", row['notes'][1]
Johannes