Corrupted images after attempting to store PNG images as BLOBs in MySQL?
fredrik at pythonware.com
Wed Aug 13 16:40:52 CEST 2008
Keith Hughitt wrote:
> I've run into a strange error while trying to store some PNG images in
> a MySQL database using MySQLdb. When I try to insert smaller images (<
> 64kb or so) everything seems to work fine. When I start trying to
> insert larger images (~150kb), however, the images get corrupted along
> the way.
> The result is that only part of the image, e.g. the top 30% of the
> image, is stored in the database, and the rest is simply transparent.
> Furthermore, if I attempt to view the image in mysql-query-browser, it
> does not display and simply states "Cannot display as image data,"
> which seems to further suggest the idea that the data is being
> corrupted somewhere along the way.
> To store the image I'm using:
> blob = open(img, 'rb').read()
> sql = "INSERT INTO table VALUES('%s')" % (MySQLdb.escape_string(blob))
Ouch. Please use parameters instead of explicit escapes and string
formatting; Python's not PHP.
> Anyone have any ideas?
Silently truncating or otherwise mangling columns is a standard MySQL
feature. What does the table definition look like?
More information about the Python-list