Problem mit Dict Cursor von psycopg2

Installation und Anwendung von Datenbankschnittstellen wie SQLite, PostgreSQL, MariaDB/MySQL, der DB-API 2.0 und sonstigen Datenbanksystemen.
Antworten
keboo
User
Beiträge: 132
Registriert: Sonntag 19. Februar 2006, 14:03

Hallo Leute!

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]

Danke für eure Hilfe,

Johannes
BlackJack

keboo hat geschrieben: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()
Wie sollte das denn mit einem Dictionary funktionieren? Du holst mit `fetchall()` *alle* Zeilen aus der Datenbank. Wie sollten die in einem Dictionary gespeichert werden?

Schau Dir doch mal die *Elemente* der Liste an.
keboo
User
Beiträge: 132
Registriert: Sonntag 19. Februar 2006, 14:03

Hi Blackjack!

Ich habe von MySQL auf PostgreSQL umgestellt.

Bei MySQLdb hat es funktioniert, dass das Ergebnis der Anfrage in ein dictionnary gschrieben wurde.

Code: Alles auswählen


import MySQLdb

# connect
db =  MySQLdb.connect(host="localhost", user="root", passwd="root", db="test")
   
# create a cursor
cursor = MySQLdb.cursors.DictCursor(db)
   
# execute SQL statement
cursor.execute("SELECT * FROM contacts)
   
# get the resultset as a tuple
result = cursor.fetchall()

Das Resultat ist dann ein Dictionnary, in welchem zu jeweilegen Tabelleneintrag als Key der Spaltenname steht.

Wäre schön, wenn diese Funktionalität auch über psycopg2 funktiert. Danke für eure Tipps.

MfG,

Johannes
BlackJack

keboo hat geschrieben:Bei MySQLdb hat es funktioniert, dass das Ergebnis der Anfrage in ein dictionnary gschrieben wurde.
So wie Du das hier beschreibst, hat das sicher nicht funktioniert.

Code: Alles auswählen


import MySQLdb

# connect
db =  MySQLdb.connect(host="localhost", user="root", passwd="root", db="test")
   
# create a cursor
cursor = MySQLdb.cursors.DictCursor(db)
   
# execute SQL statement
cursor.execute("SELECT * FROM contacts)
   
# get the resultset as a tuple
result = cursor.fetchall()
Das Resultat ist dann ein Dictionnary, in welchem zu jeweilegen Tabelleneintrag als Key der Spaltenname steht.
Nehmen wir mal an es gibt zwei Einträge in der Tabelle und es gibt eine Spalte `name` und in einer Zeile steht Hans und in der anderen steht Peter. Was sollte dann bitteschön das Ergebnis von ``print result['name']`` sein? `result` enthält *alle* Ergebnisse, also in diesem Fall den Inhalt von *zwei* Datenbankzeilen.
Wäre schön, wenn diese Funktionalität auch über psycopg2 funktiert.
Das tut sie. Du musst halt nur das gleiche machen wie in dem zweiten Quelltext aus Deinem ersten Beitrag.
Antworten