简体   繁体   中英

BIT 0 represented as '\x00'

In a database table I have a column defined as: 在此处输入图片说明

Using this query: query = text( "SELECT * FROM %s WHERE " % "aTable" "%s=%s AND " % ("done", 0) )

    result = engine.execute(query)
    row = result.fetchone()

when I call print row['done'] then I get '\\x00' .

For generation of tables I sqlacodegen which generated the done columns as this:

Column('done', BIT(1), nullable=False),

Am I missing some configuration in SqlAlchemy? I don't want to convert the hex to int everywhere I am goint to use a BIT column.

EDIT So the problem is not with sqlalchemy but it seems that pymysql is to blame.

Try to have a look to this chapter of the Python documentation .

However, in your case, it might be enough something like ord to convert your hex:

>>> a = b'\x00' 
>>> a
'\x00'
>>> str(a)
'\x00'
>>> ord(a)
0
>>> ord('\x5f')
95

The solution is to override the BIT column converter and pass it to connection of pyMysql:

from pymysql import converters
converions = converters.conversions
converions[FIELD_TYPE.BIT] = lambda x: False if '\x00' else True
return pymysql.connect(
        conv=converions,
        ...
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM