当前位置:首页 > 站长知识 > 数据库 > 正文内容

mysql数据库中存储图片和读取图片的方法

2024-11-29数据库39

方法一:将图片以 BLOB 类型存储在数据库中

MySQL 语句实现

  • 创建表
    首先,你需要在 MySQL 中创建一个包含 BLOB 字段的表来存储图片。

1
2
3
4
5
CREATE TABLE images (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    image LONGBLOB NOT NULL
);
  • 插入图片
    假设你已经有了图片数据(可能是从文件读取的二进制数据),你可以使用 INSERT INTO ... VALUES 语句来插入图片。

1
INSERT INTO images (name, image) VALUES ('example.jpg', LOAD_FILE('/path/to/example.jpg'));

注意:LOAD_FILE() 函数要求 MySQL 服务器对指定路径有读取权限,且该路径必须在 MySQL 服务器的 secure_file_priv 变量指定的目录中(如果启用了该变量)。

  • 读取图片:
    要从数据库中读取图片,你可以使用 SELECT 语句,并在应用程序中处理返回的二进制数据。

1
SELECT image FROM images WHERE id = 1;

要检索图片数据并将其保存为文件,你可以使用SELECT语句和INTO OUTFILE:

1
SELECT image FROM images WHERE id = 1 INTO OUTFILE '/path/to/your/output.jpg';
  • 注意:使用LOAD_FILE()和INTO OUTFILE需要相应的文件系统权限。此外,对于大型二进制数据,请考虑性能影响,因为BLOB数据类型可能会影响数据库性能。

Python 实现

插入图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import mysql.connector
 
# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='your_username',
    password='your_password',
    database='your_database'
)
cursor = conn.cursor()
 
# 读取图片文件
with open('/path/to/example.jpg', 'rb') as file:
    binary_data = file.read()
 
# 插入图片到数据库
sql = "INSERT INTO images (name, image) VALUES (%s, %s)"
val = ('example.jpg', binary_data)
cursor.execute(sql, val)
conn.commit()
 
# 关闭连接
cursor.close()
conn.close()

读取图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import mysql.connector
from io import BytesIO
from PIL import Image
 
# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='your_username',
    password='your_password',
    database='your_database'
)
cursor = conn.cursor()
 
# 从数据库中读取图片
sql = "SELECT image FROM images WHERE id = 1"
cursor.execute(sql)
result = cursor.fetchone()
image_data = result[0]
 
# 使用 PIL 库显示图片
image = Image.open(BytesIO(image_data))
image.show()
 
# 关闭连接
cursor.close()
conn.close()

方法二:将图片存储在文件系统中,并在数据库中存储路径

MySQL 语句实现

  • 创建表
    创建一个只包含图片路径或 URL 的表。

1
2
3
4
5
CREATE TABLE images (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    path VARCHAR(255) NOT NULL
);
  • 插入图片路径
    当图片文件被保存到文件系统后,将其路径插入到数据库中。

1
INSERT INTO images (name, path) VALUES ('example.jpg', '/path/to/example.jpg');
  • 读取图片路径
    从数据库中读取图片路径,并使用该路径在文件系统中访问图片。

1
SELECT path FROM images WHERE id = 1;
  • 注意:使用LOAD_FILE()和INTO OUTFILE需要相应的文件系统权限。此外,对于大型二进制数据,请考虑性能影响,因为BLOB数据类型可能会影响数据库性能。

Python 实现

保存图片到文件系统并插入路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import mysql.connector
 
# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='your_username',
    password='your_password',
    database='your_database'
)
cursor = conn.cursor()
 
# 保存图片到文件系统
file_path = '/path/to/save/example.jpg'
with open('/path/to/example.jpg', 'rb') as source_file:
    with open(file_path, 'wb') as dest_file:
        dest_file.write(source_file.read())
 
# 插入图片路径到数据库
sql = "INSERT INTO images (name, path) VALUES (%s, %s)"
val = ('example.jpg', file_path)
cursor.execute(sql, val)
conn.commit()
 
# 关闭连接
cursor.close()
conn.close()

读取图片路径并显示图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import mysql.connector
from PIL import Image
 
# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='your_username',
    password='your_password',
    database='your_database'
)
cursor = conn.cursor()
 
# 从数据库中读取图片路径
sql = "SELECT path FROM images WHERE id = 1"
cursor.execute(sql)
result = cursor.fetchone()
image_path = result[0]
 
# 使用 PIL 库显示图片
image = Image.open(image_path)
image.show()
 
# 关闭连接
cursor.close()
conn.close()

总结

  • BLOB 存储:适合存储小图片或需要保持数据完整性的场景,但可能会增加数据库的大小和备份复杂性。

  • 文件系统存储:适合存储大图片或需要频繁访问的场景,可以减少数据库负载,但需要确保文件系统的可用性和安全性。

选择哪种方法取决于具体的应用场景和需求。