0

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.

1 Answer 1

0

in your query:

 wquery = ("UPDATE `cin`.`flags` SET `flag`='%s', `update_time`='%d' WHERE `name`='%s' AND `lockid`='%s'") % (pic_bin, update_time, names[index], lockid)

what you do, basically, is to create a string from a binary using the string format system. There, python tries to transform each argument to a string (i.e. str(pic_bin)), and then replace the matching %s in your string with that. You should instead be using the cursor's system:

wquery = "UPDATE `cin`.`flags` SET `flag`='%s', `update_time`='%d' WHERE `name`='%s' AND `lockid`='%s'"
cursor.execute(wquery, (pic_bin, update_time, names[index], lockid))

Have a look at other questions like yours

HTH

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I understood that the variable was not used in the correct way but was not able to find over google... But I still get this error: Traceback (most recent call last): File "C:\Users...\Xflags.py", line 61, in <module> cursor.execute(wquery, (pic_bin, update_time, names[index], lockid,)) File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 381, in execute "Wrong number of arguments during string formatting") ProgrammingError: Wrong number of arguments during string formatting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.