MySQL本身是一个关系型数据库管理系统,它主要用于存储和管理结构化数据。虽然MySQL可以存储图片的二进制数据,但它并不是专门用于文件存储的解决方案。通常,图片等大文件会存储在文件系统中,而数据库中仅存储文件的路径或引用。
TINYBLOB:最大长度为255字节。BLOB:最大长度为65,535字节(约64KB)。MEDIUMBLOB:最大长度为16,777,215字节(约16MB)。LONGBLOB:最大长度为4,294,967,295字节(约4GB)。import os
import mysql.connector
# 连接到MySQL数据库
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
cursor = db.cursor()
# 上传图片
file_path = "path/to/your/image.jpg"
file_name = os.path.basename(file_path)
# 将图片保存到文件系统
with open(file_path, 'rb') as file:
image_data = file.read()
# 将图片路径保存到数据库
sql = "INSERT INTO images (file_name, file_path) VALUES (%s, %s)"
cursor.execute(sql, (file_name, file_path))
db.commit()
cursor.close()
db.close()import mysql.connector
# 连接到MySQL数据库
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
cursor = db.cursor()
# 上传图片
file_path = "path/to/your/image.jpg"
# 将图片的二进制数据直接存储到MySQL
with open(file_path, 'rb') as file:
image_data = file.read()
sql = "INSERT INTO images (file_name, image_data) VALUES (%s, %s)"
cursor.execute(sql, (os.path.basename(file_path), image_data))
db.commit()
cursor.close()
db.close()希望这些信息对你有所帮助!
没有搜到相关的文章