Python 3.7.4 pymysql 8.0.11 MySQL Community Server
以二進位格式讀取圖片
rrreee存放圖片欄位的屬性為longblog
,即long binary large object
with open("./test.jpg", "rb") as file: image = file.read()
將二進位格式的圖片資料存入MySQL
def create_image_table(self): sql = 'create table if not exists picture ( \ image longblob);' try: self.cursor.execute(sql) self.connection.commit() except pymysql.Error: print(pymysql.Error)
以二進位的格式寫出圖片
rrreeedef insert_image(self, image): sql = "insert into picture(image) values(%s)" self.cursor.execute(sql, image) self.connection.commit()
以上是怎麼用Python程式實作向MySQL存放圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!