Re: Dictionary

Antonio Prado <antonio-m7WKv7+duuT/[email protected]> Thu, 22 Apr 2010 15:39:41 -0300
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Em Qui, 2010-04-22 às 15:25 -0300, Antonio Prado escreveu:
> Em Qui, 2010-04-22 às 10:59 -0700, Tim Roberts escreveu:
> > 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]
> 
> 
> It worked, but in some cases this error that occurs:
> 
> >>> expected string or Unicode object, NoneType found
> 
> 
> What is causing the error?


[...]
cur = conection.cur(cursor_factory=psycopg_extras.DictCursor)
cur.execute("SELECT * FROM users")
result = self.cursor.fetchall() <<<<<<------------- It's correct?
result = [dict(x) for x in result]


Antonio.



_______________________________________________
Psycopg mailing list
[email protected]
http://lists.initd.org/mailman/listinfo/psycopg