Python Mysqldb returns '\ x01' for bit values

Im using Mysqldb with Python (2.7) to return a bunch of values, forms a latin1 encoded Mysql database.

The mysql database has values ​​like bit (1), when they return, they look like this: \ x01 '

Decimal values ​​are returned as this Decimal number ('0E-8')

The remaining values ​​and types are in order.

Sample database:

{'test': '\ x01', 'currency': u'bla ',' balance ': Decimal (' 0E-8 '),' first ': u'John'}

Here's how im connects to the database: self.con = MySQLdb.connect (host = conn ['host'], user = conn ['user'], passwd = conn ['passwd'], db = conn ['db' ], charset = conn ['charset'], port = int (conn ['port']), if "port" in conn else 3306)

I would like bit (1) values ​​to be returned as True or False. I looked around and think it might be MySQLdb.converters.conversions, but I'm not sure how to implement this. Any ideas would be great. Thanks

And I do not have access to changing types in the database.

+4
source share
3 answers

try:

orig_conv = MySQLdb.converters.conversions
#Adding support for bit data type
orig_conv[FIELD_TYPE.BIT] = lambda data: data == '\x01'

passing this into my connection.
MySQLdb.connect(conv=orig_conv)

using "orig_conv [FIELD_TYPE.BIT] = bool", just returned everything as true for me

+5

MySQL, , BIT .

: BIT MySQL, Baron Schwartz, High Performance MySQL.

, TINYINT 0 1 . - , BIT .

MySQL BOOL. , TINYINT(1) ( 1 ).


, SQL-:

MySQL 1 0 , :

SELECT (test=B'1') AS test, currency, balance, first FROM ...

:

SELECT IF(test=B'1', 1, 0) AS test, currency, balance, first FROM ...

SQL:

SELECT CASE test=B'1' WHEN true THEN 1 ELSE 0 END AS test, currency, balance, first FROM ...
+3

: Mysqldb.

orig_conv = MySQLdb.converters.conversions
#Adding support for bit data type
orig_conv[FIELD_TYPE.BIT] = bool

passing this into my connection.
MySQLdb.connect(conv=orig_conv)

FIELD_TYPE MySQLdb.constants

from MySQLdb.constants import FIELD_TYPE
+1
source

All Articles