getting floats instead of decimals from session.execute

1,191 views
Skip to first unread message

Chris Withers

unread,
Oct 13, 2010, 10:01:50 AM10/13/10
to sqlal...@googlegroups.com
Hi All,

If I'm doing:

session.execute('select some_numeric from some_table')

...how do I get floats back in the result instead of decimals?

Chris

Michael Bayer

unread,
Oct 13, 2010, 5:37:09 PM10/13/10
to sqlal...@googlegroups.com
use text() with Numeric(as_decimal=False)

> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>

Chris Withers

unread,
Oct 13, 2010, 8:04:06 PM10/13/10
to sqlal...@googlegroups.com
On 13/10/2010 22:37, Michael Bayer wrote:
> use text() with Numeric(as_decimal=False)

I can't quite join the dots on this one...

I get passed a bunch of sql to execute, I don't have control over that.
Where do I wire in Numeric(as_decimal=False) and how do I tell where I
need to given an arbitrary select as a string of sql?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Michael Bayer

unread,
Oct 14, 2010, 12:10:06 PM10/14/10
to sqlal...@googlegroups.com

On Oct 13, 2010, at 8:04 PM, Chris Withers wrote:

> On 13/10/2010 22:37, Michael Bayer wrote:
>> use text() with Numeric(as_decimal=False)
>
> I can't quite join the dots on this one...
>
> I get passed a bunch of sql to execute, I don't have control over that.
> Where do I wire in Numeric(as_decimal=False) and how do I tell where I need to given an arbitrary select as a string of sql?

text() is in the docs, including an example of typemap:

http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=text#sqlalchemy.sql.expression.text

Chris Withers

unread,
Oct 14, 2010, 1:35:45 PM10/14/10
to sqlal...@googlegroups.com, Michael Bayer
On 14/10/2010 17:10, Michael Bayer wrote:
>
>> I get passed a bunch of sql to execute, I don't have control over that.
>> Where do I wire in Numeric(as_decimal=False) and how do I tell where I need to given an arbitrary select as a string of sql?
>
> text() is in the docs, including an example of typemap:
>
> http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=text#sqlalchemy.sql.expression.text

Okay, but I don't see how a typemap helps.
All I'm getting is a string of sql, I don't know what column names are
included in that, and even if I did, I wouldn't know the schema of the
table to know what they should be mapped to.

How can I influence what column type is mapped to a particular python
type without knowing the names or types of the columns?

cheers,

Chris

Michael Bayer

unread,
Oct 14, 2010, 1:49:07 PM10/14/10
to Chris Withers, sqlal...@googlegroups.com

On Oct 14, 2010, at 1:35 PM, Chris Withers wrote:

> On 14/10/2010 17:10, Michael Bayer wrote:
>>
>>> I get passed a bunch of sql to execute, I don't have control over that.
>>> Where do I wire in Numeric(as_decimal=False) and how do I tell where I need to given an arbitrary select as a string of sql?
>>
>> text() is in the docs, including an example of typemap:
>>
>> http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=text#sqlalchemy.sql.expression.text
>
> Okay, but I don't see how a typemap helps.
> All I'm getting is a string of sql, I don't know what column names are included in that,

If you don't know your columns, neither does SQLAlchemy. Depending on your DBAPI you may be able to get the DBAPI itself to not use the Decimal type, see the docs on your DBAPI for information on type coercion at the DBAPI level.

Otherwise, you'd filter your result set, do isinstance(x, Decimal), convert to float. You may be able to use result.cursor.description if the DBAPI returns reasonable types. We could someday add an execution_option() that allows a map of DBAPI types mapped to result types to be passed.

Reply all
Reply to author
Forward
0 new messages