Python manipulates how MySQL reads pictures from the database
This article is about how Python operates MySQL to read pictures from a database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Read the picture from the database
Import MySQLdb as mdbimport systry:# connects to mysql and gets the connected object conn = mdb.connect ('localhost',' root', 'root',' test') Cursor = conn.cursor () # query the SQLcursor.execute ("SELECT Data FROM Images LIMIT 1") of the picture field # use the binary write method to open an image file If it does not exist, automatically create fout = open ('image.png','wb') # directly send data such as file fout.write (cursor.fetchone () [0]) # close the written file fout.close () # release the resource cursor.close () conn.close () except IOError for querying the data, and EVL # catch the exception of IO The main reason is that there will be an error in file writing print "Error% d:% s"% (e.args [0], e.args [1]) sys.exit (1) Thank you for reading! On "Python operation MySQL how to read pictures from the database" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!