Re: Dictionary

Antonio Prado <antonio-m7WKv7+duuT/[email protected]> Thu, 22 Apr 2010 16:07:04 -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?


The problem is caused by the code below.

This code was created with the intention of returning type float instead
of type Decimal.

What is wrong?


import psycopg2
psycopg=psycopg2
import psycopg2.extras
psycopg_extras = psycopg2.extras
-----------------------------------------------------------
import psycopg2.extensions
DEC2FLOAT = psycopg2.extensions.new_type(
psycopg2._psycopg.DECIMAL.values, # oids for the decimal type
      'DEC2FLOAT', # the new typecaster name
        psycopg2.extensions.FLOAT) # the typecaster creating floats

# register the typecaster globally
psycopg2.extensions.register_type(DEC2FLOAT)
------------------------------------------------------------
conection = psycopg.connect(...
conection.set_isolation_level(1)


[]'s

Antonio.




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