最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python程序?qū)崿F(xiàn)向MySQL存放圖片

 更新時間:2023年03月14日 08:44:17   作者:I_am_overflow  
這篇文章主要介紹了Python程序?qū)崿F(xiàn)向MySQL存放圖片,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

環(huán)境

Python 3.7.4
pymysql
8.0.11 MySQL Community Server

讀取圖片

以二進(jìn)制格式讀取圖片

with open("./test.jpg", "rb") as file:
	image = file.read()

創(chuàng)建存放圖片的表

存放圖片字段的屬性為longblog,即long binary large object

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)

存入MySQL

將二進(jìn)制格式的圖片數(shù)據(jù)存入MySQL

def insert_image(self, image):
    sql = "insert into picture(image) values(%s)"
    self.cursor.execute(sql, image)
    self.connection.commit()

保存MySQL查詢得到的圖片數(shù)據(jù)為圖片

以二進(jìn)制的格式寫出圖片

def get_image(self, path):
    sql = 'select * from picture'
    try:
        self.cursor.execute(sql)
        image = self.cursor.fetchone()[0]
        with open(path, "wb") as file:
            file.write(image)
    except pymysql.Error:
        print(pymysql.Error)
    except IOError:
        print(IOError)

實現(xiàn)代碼

import pymysql


class Database():
	
	'''
		Description:
			database demo to store image in MySQL RDBMS
		Attributes:
			None
	'''
    
    def __init__(self):
        self.connection = pymysql.connect(host='<host name>',user='<user name>',passwd='<password>',db='<database name>',charset='utf8')
        self.cursor = self.connection.cursor()

	'''
		Description:
			create table to store images
		Args:
			None
		Return:
			None
	'''
    
    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)
	
	'''
		Description:
			insert image into table
		Args:
			image:
				image to store
		Returns:
			None
	'''

    def insert_image(self, image):
        sql = "insert into picture(image) values(%s)"
        self.cursor.execute(sql, image)
        self.connection.commit()
	
	'''
		Description:
			get image from database
		Args:
			path:
				path to save image
		Returns:
			None
	'''	

    def get_image(self, path):
        sql = 'select * from picture'
        try:
            self.cursor.execute(sql)
            image = self.cursor.fetchone()[0]
            with open(path, "wb") as file:
                file.write(image)
        except pymysql.Error:
            print(pymysql.Error)
        except IOError:
            print(IOError)
            
	'''
		Description:
			destruction method
		Args:
			None
		Returns:
			None
	'''
	
    def __del__(self):
        self.connection.close()
        self.cursor.close()

if __name__ == "__main__":
    database = Database()
    # read image from current directory
    with open("./test.jpg", "rb") as file:
        image = file.read()

    database.create_image_table()
    database.insert_image(image)

    database.get_image('./result.jpg')

測試結(jié)果

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

广水市| 巴楚县| 武平县| 深圳市| 青铜峡市| 长丰县| 清远市| 天全县| 哈尔滨市| 忻城县| 离岛区| 许昌县| 广东省| 龙海市| 揭阳市| 荆州市| 麦盖提县| 辽源市| 松阳县| 闵行区| 大港区| 南汇区| 阿勒泰市| 霍邱县| 余庆县| 枣强县| 福泉市| 丹棱县| 铜鼓县| 体育| 诸城市| 南陵县| 许昌市| 中宁县| 翼城县| 宁南县| 台南市| 石楼县| 临洮县| 梁河县| 渝中区|