保存图片到MySQL&从MySQL读取图片
时间: 2024-04-05 14:12:38 浏览: 86
在MySQL中保存图片可以使用BLOB类型,BLOB类型是二进制类型,可以存储各种二进制数据,包括图片、音频、视频等。
1. 保存图片到MySQL
使用Python中的MySQL Connector库连接MySQL数据库,将图片转换为二进制数据,然后将二进制数据存储到MySQL数据库中。
```python
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
# 读取图片并转换为二进制数据
with open("image.jpg", "rb") as image_file:
image_data = image_file.read()
try:
# 连接MySQL数据库
connection = mysql.connector.connect(
host='localhost',
database='test',
user='root',
password='password')
# 插入二进制数据到MySQL数据库中
cursor = connection.cursor()
sql_insert_blob_query = """ INSERT INTO images
(id, image) VALUES (%s,%s)"""
insert_blob_tuple = (1, image_data)
result = cursor.execute(sql_insert_blob_query, insert_blob_tuple)
connection.commit()
print("Image inserted successfully as a BLOB into MySQL database")
except mysql.connector.Error as error:
print("Failed inserting BLOB data into MySQL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
```
在上面的例子中,首先使用Python中的open()函数读取图片,并将其转换为二进制数据。然后,使用MySQL Connector库连接MySQL数据库,使用INSERT语句将二进制数据插入到BLOB类型的列中。
2. 从MySQL读取图片
从MySQL数据库读取图片也很简单,只需使用SELECT查询语句,然后将查询结果转换为二进制数据,再将其写入到文件中即可。
```python
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
# 连接MySQL数据库
connection = mysql.connector.connect(
host='localhost',
database='test',
user='root',
password='password')
# 查询BLOB类型的数据
cursor = connection.cursor()
sql_fetch_blob_query = """SELECT image from images where id = %s"""
cursor.execute(sql_fetch_blob_query, (1,))
record = cursor.fetchone()
image = record[0]
# 将二进制数据写入到文件中
with open("image.jpg", "wb") as file:
file.write(image)
print("Image extracted successfully from MySQL database")
except mysql.connector.Error as error:
print("Failed reading BLOB data from MySQL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
```
在上面的例子中,首先使用SELECT查询语句查询BLOB类型的数据,然后将查询结果转换为二进制数据。接着,使用open()函数将二进制数据写入到文件中。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)