Re: Dictionary

Tim Roberts <[email protected]> Thu, 22 Apr 2010 10:59:50 -0700
Newsgroups gmane.comp.python.db.psycopg.devel
Organization Providenza & Boekelheide, Inc.
Message-ID <[email protected]>
Antonio Prado wrote:
> Correct! That's right I'm trying to do.
>
> But I'm having problem bellow.
>
> What is wrong?
>
> [...]
> cur = db.cur(cursor_factory=psycopg_extras.DictCursor)
> cur.execute("SELECT * FROM users")
> result = cur.fetchall()
>
>   
>>>> print result
>>>>         
> [[2, 'JOSE ALBERTO', 'MOTA'], [3, 'MARY ANN', 'MARY']]
>
>   
>>>> print resultado[0]['nome']
>>>>         
> JOSE ALBERTO
>
>   
>>>> resultado[0]['nome'] = 'teste'
>>>>         
> TypeError: list indices must be integers, not str
>   

Each row behaves like a read-only dictionary, although it is not a
dictionary.

> result = dict(result)
>   
>>> ValueError: dictionary update sequence element #0 has length 3; 2 is
>>>       
> required
>   

You are trying to convert the whole result set here, not just one row. 
You need to convert the rows one by one.  For example
    result = [dict(x) for x in result]

-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.