title: upload web files to mysql in python
body: <p>I am new to python and looking for a way to execute a "complicated" task.</p>
  
  <p>I need to store small images (less than 1mb each) into the <code>Image</code> 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).</p>
  
  <p>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.</p>
  
  <pre><code>codes = [xx, aa, ab] # This line is simulated as it is gerated by a long script run previously...
  
  print "-&gt; 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 "-&gt; Flags downloaded"
  </code></pre>
  
  <p>This code return error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\***\flagstest.py", line 61, in &lt;module&gt;
      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)
  </code></pre>
  
  <p>Hope anybody can help me find a way to work arround it...</p>
  
  <p>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.</p>