From 431920b3676ffecc80fd7f0bc25d774d6e5a2aa1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 14 Apr 2010 01:05:36 +0100 Subject: [PATCH] Added recipe to get Python floats from Postgres decimals in the FAQ. --- doc/src/faq.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/doc/src/faq.rst b/doc/src/faq.rst index b45d1032..8015864d 100644 --- a/doc/src/faq.rst +++ b/doc/src/faq.rst @@ -70,6 +70,20 @@ My database is Unicode, but I receive all the strings as UTF-8 `str`. Can I rece See :ref:`unicode-handling` for the gory details. +Psycopg converts :sql:`decimal`\/\ :sql:`numeric` database types into Python `!Decimal` objects. Can I have `!float` instead? + You can register the `~psycopg2.extensions.FLOAT` typecaster to be used in + place of `~psycopg2.extensions.DECIMAL`:: + + DEC2FLOAT = psycopg2.extensions.new_type( + psycopg2.extensions.DECIMAL.values, + 'DEC2FLOAT', + psycopg2.extensions.FLOAT) + psycopg2.extensions.register_type(DEC2FLOAT) + + See :ref:`type-casting-from-sql-to-python` to read the relevant + documentation. If you find `!psycopg2.extensions.DECIMAL` not avalable, use + `!psycopg2._psycopg.DECIMAL` instead. + I can't compile `!psycopg2`: the compiler says *error: Python.h: No such file or directory*. What am I missing? You need to install a Python development package: it is usually called ``python-dev``.