Re: Decimal

Antonio Prado <antonio-m7WKv7+duuT/[email protected]> Thu, 22 Apr 2010 18:32:30 -0300
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Em Ter, 2010-04-20 às 14:40 -0300, Antonio Prado escreveu:
> Em Ter, 2010-04-13 às 23:24 +0100, Daniele Varrazzo escreveu:
> > On Tue, Apr 13, 2010 at 8:10 PM, Antonio Prado <[email protected]> wrote:
> > > Hello!
> > >
> > > Using psycopg2 to connect as PostgreSQL, when set
> > > cur = conn.cursor (cursor_factory = psycopg2.extras.DictCursor)
> > > is returning data in decimal format.
> > >
> > > In my database I have numeric and decimal types.
> > >
> > > How to always return Float or Integer, equal in psycopg1?
> > 
> > You can register FLOAT as the type caster for the decimal type. You
> > should execute this code just once in your app:
> > 
> > # create a typecaster from Postgres decimal to Python float
> > 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)
> > 
> > # Now Postgres decimals will produce Python floats
> > cnn = psycopg2.connect(database="test")
> > cur = cnn.cursor()
> > cur.execute("SELECT '123.45'::decimal(10,2)")
> > n = cur.fetchone()[0]
> > print n, type(n)
> > 123.45, <type 'float'>

[...]
Using the solution above error occurs on record content Null.

How to fix this?


Example:

# create a typecaster from Postgres decimal to Python float
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)

# Now Postgres decimals will produce Python floats
cnn = psycopg2.connect(database="jbm", user='postgres')
cur = cnn.cursor()
cur.execute("SELECT Null::decimal(10,2)")  # <<<<---------REGISTER NULL
n = cur.fetchone()[0]
print n, type(n)

>>> None <type 'NoneType'>
>>> Exception TypeError: 'expected string or Unicode object, NoneType
found'




[]'s
Antonio Prado.





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