1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。
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 变量指定的目录中(如果启用了该变量)。
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' ; |
插入图片:
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() |
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; |
保存图片到文件系统并插入路径:
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() |
选择哪种方法取决于具体的应用场景和需求。
以上就是MySQL数据库中存储图片和读取图片的操作代码的详细内容。
学习资料见知识星球。
以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。
快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利!
更多技巧, www.excelbook.cn
欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;
1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。