mysql数据库中存储图片和读取图片的方法
方法一:将图片以 BLOB 类型存储在数据库中
MySQL 语句实现
1 2 3 4 5 | CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR (255) NOT NULL ,
image LONGBLOB NOT NULL
);
|
1 | INSERT INTO images ( name , image) VALUES ( 'example.jpg' , LOAD_FILE( '/path/to/example.jpg' ));
|
注意:LOAD_FILE() 函数要求 MySQL 服务器对指定路径有读取权限,且该路径必须在 MySQL 服务器的 secure_file_priv 变量指定的目录中(如果启用了该变量)。
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' ;
|
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 ]
image = Image. open (BytesIO(image_data))
image.show()
cursor.close()
conn.close()
|
方法二:将图片存储在文件系统中,并在数据库中存储路径
MySQL 语句实现
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;
|
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 ]
image = Image. open (image_path)
image.show()
cursor.close()
conn.close()
|
总结
选择哪种方法取决于具体的应用场景和需求。