I am new to python and looking for a way to execute a "complicated" task.
I need to store small images (less than 1mb each) into the Image column (blob) of my DB. My script currently retrieve URLs required to read the files. I've found no way so far to retrieve the images and upload them to the DB without writing them the the HDD (to spare the HDD).
I am currently using URLlib2 and MySQL Connector, and I'd like to remain if possible with them as they work on both Windows and Debian.
codes = [xx, aa, ab] # This line is simulated as it is gerated by a long script run previously...
print "-> Downloading Flags..."
for code in codes:
if not code == 'xx':
filename = "%s-lgflag.gif" % code
url = "%s%s" % (flagurl, filename)
index = code.index(code)
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open(url)
pic = infile.read()
update_time = int(time.time())
wquery = ("UPDATE `cin`.`flags` SET `flag`='%s', `update_time`='%d' WHERE `name`='%s' AND `lockid`='%s'") % (pic_bin, update_time, names[index], lockid)
cursor.execute(wquery)
cnx.commit()
sys.exit() ### Exit to make sure that I don't pass the whole list of 500+ flags
print "-> Flags downloaded"
This code return error:
Traceback (most recent call last):
File "C:\Users\***\flagstest.py", line 61, in <module>
wquery = ("UPDATE `cin`.`flags` SET `flag`='%s', `update_time`='%d' WHERE `name`='%s' AND `lockid`='%s'") % (pic, update_time, names[index], lockid)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc1 in position 41: ordinal not in range(128)
Hope anybody can help me find a way to work arround it...
PS: I know storing images in DB is not the best solution, but as the DB is sync across devices and some may require offline data access, these images need to be stored there.