When I select a single column, can I prevent getting a list of one-element tuples?

From: "W(dot) Matthew Wilson" <matt(at)tplus1(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: When I select a single column, can I prevent getting a list of one-element tuples?
Date: 2012-06-17 15:55:15
Message-ID: CAGHfCUA8U6_VvdsmfafA7EHqrU68L+2Xz-eKyJpnMBqESW6aHg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

I have some code that looks sort of like this::

cursor.execute("""
select user_email_address
from my_user_table
""")

my_users = cursor.fetchall()

and my_users is a list of one-element lists.

Now I have to index into that inner list on every row when I want to
print my results like this::

for row in my_users:
print row[0]

Or if I use the excellent dictcursor from the extras package I can do
this instead::

for row in my_users:
print row['user_email_address']

This is not a huge problem, but it irritates me because I often forget
about it and then get an error and have to go back and fix it.

I understand that when I ask for more than one column in a query,
psycopg2 MUST return each row as a list. But when I am asking for
exactly one column, is there a way to tell psycopg2 to not return a
list for every row, but just the single value?

Thanks for the help.

Matt

--
W. Matthew Wilson
matt(at)tplus1(dot)com
http://tplus1.com

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2012-06-17 16:32:58 Re: When I select a single column, can I prevent getting a list of one-element tuples?
Previous Message Bill House 2012-06-17 15:32:29 Capacity questions