怎么用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')
测试结果
以上是怎么用Python程序实现向MySQL存放图片的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可以通过以下步骤打开 phpMyAdmin:1. 登录网站控制面板;2. 找到并点击 phpMyAdmin 图标;3. 输入 MySQL 凭据;4. 点击 "登录"。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

如何使用 phpMyAdmin 连接到 MySQL?访问 phpMyAdmin 的 URL,通常为 http://localhost/phpmyadmin 或 http://[您的服务器 IP 地址]/phpmyadmin。输入您的 MySQL 用户名和密码。选择您要连接的数据库。点击 "连接" 按钮以建立连接。

问题:如何查看 Redis 服务器版本?使用命令行工具 redis-cli --version 查看已连接服务器的版本。使用 INFO server 命令查看服务器内部版本,需解析返回信息。在集群环境下,检查每个节点的版本一致性,可使用脚本自动化检查。使用脚本自动化查看版本,例如用 Python 脚本连接并打印版本信息。

启动 Redis 服务器的步骤包括:根据操作系统安装 Redis。通过 redis-server(Linux/macOS)或 redis-server.exe(Windows)启动 Redis 服务。使用 redis-cli ping(Linux/macOS)或 redis-cli.exe ping(Windows)命令检查服务状态。使用 Redis 客户端,如 redis-cli、Python 或 Node.js,访问服务器。

要从 Redis 读取队列,需要获取队列名称、使用 LPOP 命令读取元素,并处理空队列。具体步骤如下:获取队列名称:以 "queue:" 前缀命名,如 "queue:my-queue"。使用 LPOP 命令:从队列头部弹出元素并返回其值,如 LPOP queue:my-queue。处理空队列:如果队列为空,LPOP 返回 nil,可先检查队列是否存在再读取元素。

选择MySQL的原因是其性能、可靠性、易用性和社区支持。1.MySQL提供高效的数据存储和检索功能,支持多种数据类型和高级查询操作。2.采用客户端-服务器架构和多种存储引擎,支持事务和查询优化。3.易于使用,支持多种操作系统和编程语言。4.拥有强大的社区支持,提供丰富的资源和解决方案。

phpMyAdmin不仅仅是数据库管理工具,它能让你深入理解MySQL,提升编程技巧。核心功能包括CRUD和SQL查询执行,理解SQL语句的原理至关重要。高级技巧包括导出/导入数据和权限管理,需要深入的安全理解。潜在问题包括SQL注入,解决方案是参数化查询和备份。性能优化涉及SQL语句优化和索引使用。最佳实践强调代码规范、安全实践和定期备份。
