dict_cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) dict_cur.execute("SELECT column1, column2, column3 FROM mytable") result = dict_cur.fetchall() print result[0] >>> {'column2':10, 'column1':12, 'column3':42}
How can I keep column ordering without parsing SQL? It works well with the normal cursor when the list is returned, but I need access to the dictionary keys, and so you need to use RealDictCursor.
EDIT : Well, I really can't. the cursor object description attribute should be used to get the column names.
kerma
source share