MySQL-for-Python-3 icon indicating copy to clipboard operation
MySQL-for-Python-3 copied to clipboard

Blob as bytes or bytearray?

Open lucianobarcaro opened this issue 10 years ago • 2 comments

Hi, I'm trying to use mysql-p3 and I saw that blobs (images in my case) are fetched as str. Which is the best way to get blobs into bytes (old str in python2?)

lucianobarcaro avatar Feb 26 '15 12:02 lucianobarcaro

I don't really remember specifics... but it probably should be binary string/bytes if that's not so then it might be a bug. anyway from one to another you can convert with encode/decode

b'binary string'.decode('ascii') # to str
'text string'.encode('ascii') # to bytes

you can also try utf-8 encoding, it was really long time ago I worked on this... you might have to try to debug it a bit more and see if there might be a bug regarding this.

davispuh avatar Feb 26 '15 14:02 davispuh

I did a workaround (don't know if my solution is the right one or if I did at the right place - but it works).

What I did in _mysql.c (briefly): added another parameter in _mysql_field_to_python indicating if field has binary flag or not. and if it has, returns PyBytes_FromStringAndSize.

lucianobarcaro avatar Mar 04 '15 12:03 lucianobarcaro