Python程序实现向MySQL存放图片
作者:I_am_overflow 发布时间:2022-12-02 13:34:38
标签:Python,MySQL,存放图片
环境
Python 3.7.4
pymysql
8.0.11 MySQL Community Server
读取图片
以二进制格式读取图片
with open("./test.jpg", "rb") as file:
image = file.read()
创建存放图片的表
存放图片字段的属性为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
将二进制格式的图片数据存入MySQL
def insert_image(self, image):
sql = "insert into picture(image) values(%s)"
self.cursor.execute(sql, image)
self.connection.commit()
保存MySQL查询得到的图片数据为图片
以二进制的格式写出图片
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)
实现代码
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')
测试结果
来源:https://blog.csdn.net/qq_44486439/article/details/109003727


猜你喜欢
- 今天有个需要需要传递中文参数给URL但是在GBK环境下的脚本传递GBK的参数老是给我报UNICODE的解码错误。烦的很。所以我们果断选择用u
- 一、起源 因子分析的起源是这样的:1904年英
- 有个简单的查看方法,打开记事本,如要查看“Chr("119") w”,可以按下Alt+119 (先按住Alt不放,然后输
- 一、CONCAT函数concat函数是将多个字段或字符串拼接为一个字符串;但是字符串之间没有任何分隔。concat函数官方介绍-- CONC
- 动机一些bug由于本地环境和线上环境的不一致可能导致本地无法复现本地依赖和线上依赖版本不一致也可以导致一些问题有时一些bug跟数据相关,本地
- 建立资料表:Step1首先开启phpmyadmin,进入wordpress资料库中,并新增一个wp_gbook的资料表与栏位数目8。Step
- 我就废话不多说了,大家还是直接看代码吧~代码如下type KDRespBody struct { Errcode int `j
- 方法一、尽量使用复杂的SQL来代替简单的一堆 SQL.同样的事务,一个复杂的SQL完成的效率高于一堆简单SQL完成的效率。有多个查询时,要善
- 最近开发的微信公众号项目中(项目采用Vue + Vux 构建,站点部署在IIS8.5上),遇到个非常奇葩的问题,发布站点内容后,通过微信打开
- python 里面与时间有关的模块主要是 time 和 datetime如果想获取系统当前时间戳:time.time(),是一个float型
- 上一篇介绍了 HTML5 中 Canvas 的路径,这篇将要介绍一下 Canvas&nbs
- 前段时间写了一个百度图片下载器,结果发现有很多人需要使用。说实话之前写的那一款百度图片下载器比较LOW,今天刚好有时间就做了一下升级。更新了
- CocosCreator版本:2.3.4cocos没有List组件,所以要自己写。从cocos的example项目中找到assets/cas
- 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选
- 在asp中获取当前的地址栏网址很简单,使用下面这句语句即能实现获取网站域名Request.ServerVariables("HTT
- 首先,你要知道pycharm可以通过ssh链接到远程服务器,并且也能够用pycharm运行远程服务器的代码。可以参考https://www.
- 2。date_default_timezone_set("PRC"); 3。PHP&
- 有2种方法:一、 QML中定义一个信号,连接Python里的函数;这里的函数不用特意指明为槽函数,普通函数即可。QML的信号连接Python
- 'subject 信息标题 'company 发布信息的公司名称 'cont
- 一、前言大多数编译型语言,变量在使用前必须先声明,其中C语言更加苛刻:变量声明必须位于代码块最开始,且在任何其他语句之前。其他语言,想C++