Suppose I'm using requests to open an image url and read the data, save as BLOB data to MySQL, what is the most efficient way of doing it?
I did some search and looks like PIL.Image & StringIO method is recommended, as in this document : http://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content
There are some other methods, as mentioned in this question : How do I read image data from a URL in Python?
However, I seem to be using a "simpler" way of implementing the task, I "think" the code works fine, since from my datbase I'm able to open the saved image. So I shouldn't be worried about it... However, I'm not sure whether there are potential problems (since it's not suggested anywhere, not as I'm aware of)... Could someone help me look at the following code and let me know if it is problematic.
import requests
import pymysql
import pymysql.cursor
connection = pymysql.connect(user='root',host='localhost',database='mysql')
mycursor=connection.cursor() ## connect to database
pic_url='http://example_photo.jpg'
data=requests.get(pic_url) # read image
photo=data.content
sql=""" INSERT INTO table_photo (url, photo) VALUES (%s, %s)"""
mycursor.execute(sql, (pic_url, photo))
connection.commit() # save to database