最新国产好看的视频,伊人天堂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)

實(shí)現(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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

林西县| 辉南县| 乌鲁木齐县| 大同县| 芜湖市| 榕江县| 武汉市| 通河县| 内黄县| 洞口县| 余庆县| 曲阜市| 富阳市| 宾阳县| 西平县| 原阳县| 金堂县| 中牟县| 湛江市| 集贤县| 南京市| 友谊县| 临夏市| 黑水县| 连云港市| 芦溪县| 师宗县| 东兰县| 宝丰县| 疏勒县| 乌审旗| 巢湖市| 平阴县| 江陵县| 六枝特区| 莱州市| 卢氏县| 开阳县| 浪卡子县| 朝阳县| 新兴县|