MySQL是一种关系型数据库管理系统,用于存储和管理数据。在MySQL中导入图片,通常是将图片文件以二进制形式存储在数据库的BLOB(Binary Large Object)类型字段中。
MySQL中存储图片主要使用BLOB类型,包括:
CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
image BLOB
);import mysql.connector
from mysql.connector import Error
def read_image(file_path):
with open(file_path, 'rb') as file:
return file.read()
image_data = read_image('path_to_image.jpg')try:
connection = mysql.connector.connect(host='localhost',
database='your_database',
user='your_username',
password='your_password')
cursor = connection.cursor()
insert_query = "INSERT INTO images (name, image) VALUES (%s, %s)"
cursor.execute(insert_query, ('image_name.jpg', image_data))
connection.commit()
print("Image inserted successfully")
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")希望这些信息对你有所帮助!
没有搜到相关的沙龙