Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DictCursor doesn't seem to work under psycopg2

I haven't worked with psycopg2 before but I'm trying to change the cursor factory to DictCursor so that fetchall or fetchone will return a dictionary instead of a list.

I created a test script to make things simple and only test this functionality. Here's my little bit of code that I feel should work

import psycopg2 import psycopg2.extras  conn = psycopg2.connect("dbname=%s user=%s password=%s" % (DATABASE, USERNAME, PASSWORD))  cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor) cur.execute("SELECT * from review")  res = cur.fetchall()  print type(res) print res 

The res variable is always a list and not a dictionary as I would expect.

A current workaround that I've implemented is to use this function that builds a dictionary and run each row returned by fetchall through it.

def build_dict(cursor, row):     x = {}     for key,col in enumerate(cursor.description):         x[col[0]] = row[key]     return d 

Python is version 2.6.7 and psycopg2 is version 2.4.2.

like image 959
Jim Avatar asked Jul 18 '11 20:07

Jim


People also ask

Is Psycopg2 connection thread safe?

Thread and process safetyThe Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors.

What is Psycopg2 cursor?

class cursor. Allows Python code to execute PostgreSQL command in a database session. Cursors are created by the connection. cursor() method: they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection.

What is RealDictCursor?

AFAIU and from docs, RealDictCursor is a specialized DictCursor that enables to access columns only from keys (aka columns name), whereas DictCursor enables to access data both from keys or index number.

Why do we use Psycopg2?

Psycopg2 is a DB API 2.0 compliant PostgreSQL driver that is actively developed. It is designed for multi-threaded applications and manages its own connection pool.


2 Answers

Use RealDictCursor:

import psycopg2.extras  cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) cur.execute("SELECT * from review") res = cur.fetchall()     

This gives you a list with rows as real python dictionaries instead of "advanced psycopg2 list".

like image 68
kerma Avatar answered Sep 28 '22 05:09

kerma


res = cur.fetchall() 

makes res a list of psycopg2.extras.DictRows.


Alternatively, instead of calling cur.fetchall you can take advantage of the fact that cur is an iterable:

cur.execute("SELECT * from review") for row in cur:     print(row['column_name']) 

and thus you'll be able to access the data with dict-like syntax.

like image 38
unutbu Avatar answered Sep 28 '22 06:09

unutbu