Function that returns a tuple

David Wahler dwahler at gmail.com
Sun Jun 17 06:08:16 EDT 2007


On 6/17/07, Marcpp <marcpp at gmail.com> wrote:
> On 17 jun, 03:53, Dan Hipschman <d... at linux.ucla.edu> wrote:
> Hi, I need to return a tupla like this function:
>
>     def BDllids(a):
>         a = ()
>         conn = sqlite.connect('tasques.db')
>         cursor =  conn.cursor()
>         cursor.execute('SELECT * FROM tasques')
>         for row in cursor:
>             a.append (row[0])
>         return a()
>
> I'm doing the correct, method?

Tuples are immutable (can't be modified once created). Try this:

def BDllids():
    conn = sqlite.connect('tasques.db')
    cursor =  conn.cursor()
    cursor.execute('SELECT * FROM tasques')
    a = [row[0] for row in cursor]
    return tuple(a)

Is there some particular reason you need to return a tuple as opposed to a list?

P.S. Accessing fields of a SELECT * by numeric index is prone to
breakage if the order of your fields changes. You can make your code
more robust by either specifying the column name explicitly in the
SELECT statement. Alternatively, you may be interested in the
sqlite.Row object (see the documentation for details).

-- David



More information about the Python-list mailing list