You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.6 KiB
132 lines
5.6 KiB
import pymysql.cursors
|
|
import tkinter as tk
|
|
from tkinter import filedialog,messagebox
|
|
import os
|
|
|
|
# 数据库连接参数
|
|
DB_HOST = 'localhost' # 数据库主机
|
|
DB_USER = 'root' # 数据库用户名
|
|
DB_PASSWORD = '123456' # 数据库密码
|
|
DB_NAME = 'file' # 数据库名
|
|
|
|
# 创建表的SQL语句
|
|
CREATE_TABLE_SQL = """
|
|
CREATE TABLE IF NOT EXISTS images (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
filename VARCHAR(255),
|
|
content LONGBLOB,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
"""
|
|
|
|
def create_table():
|
|
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
cursor.execute(CREATE_TABLE_SQL)
|
|
connection.commit()
|
|
finally:
|
|
connection.close()
|
|
def path_to_bytes():
|
|
return filedialog.askopenfilename(filetypes=(("Image Files", "*.jpg;*.png;*.gif"), ("All Files", "*.*")))
|
|
def upload_image():
|
|
# 创建数据库表
|
|
create_table()
|
|
|
|
# 选择要上传的图像文件
|
|
selected_image_path = path_to_bytes()
|
|
if selected_image_path:
|
|
with open(selected_image_path, 'rb') as image_file:
|
|
image_content = image_file.read()
|
|
|
|
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
sql = "INSERT INTO images (filename, content) VALUES (%s, %s)"
|
|
cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content)))
|
|
connection.commit()
|
|
messagebox.showinfo("上传成功", f"图片路径: '{selected_image_path}' 上传成功.")
|
|
except Exception as e:
|
|
messagebox.showerror("上传错误", f"图片上传失败: {e}")
|
|
finally:
|
|
connection.close()
|
|
|
|
def delete_image():
|
|
view_all_images_info()
|
|
# 获取用户输入的图像ID
|
|
image_id = tk.simpledialog.askinteger("删除图片", "请输入需要删除的图像ID:")
|
|
if image_id is not None:
|
|
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
# 直接执行删除操作
|
|
cursor.execute("DELETE FROM images WHERE id = %s", (image_id,))
|
|
connection.commit()
|
|
|
|
# 提示用户确认删除
|
|
confirmation = tk.messagebox.askyesno("Confirm Delete", f"是否删除图片ID为 {image_id}的图片?")
|
|
if confirmation:
|
|
messagebox.showinfo("删除成功", f"成功删除图片ID为 {image_id} 的图片.")
|
|
else:
|
|
messagebox.showinfo("操作取消", "图片删除操作已取消。")
|
|
except Exception as e:
|
|
messagebox.showerror("删除错误", f"图片删除失败: {e}")
|
|
finally:
|
|
connection.close()
|
|
|
|
def download_image():
|
|
view_all_images_info()
|
|
# 获取用户输入的图像ID
|
|
image_id = tk.simpledialog.askinteger("下载图片", "请输入需要下载的图像ID:")
|
|
if image_id is not None:
|
|
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
# 查询图像内容和文件名
|
|
cursor.execute("SELECT content, filename FROM images WHERE id = %s", (image_id,))
|
|
image_data = cursor.fetchone()
|
|
if image_data:
|
|
content, original_filename = image_data
|
|
# 构建默认的保存文件名
|
|
default_filename = os.path.splitext(original_filename)[0] + ".jpg"
|
|
# 使用默认扩展名并允许用户选择保存位置
|
|
save_path = filedialog.asksaveasfilename(initialfile=default_filename, defaultextension=".jpg", filetypes=[("JPEG Images", "*.jpg"), ("All Files", "*.*")])
|
|
if save_path:
|
|
try:
|
|
with open(save_path, 'wb') as image_file:
|
|
image_file.write(content)
|
|
messagebox.showinfo("成功", f"图片已成功下载到 '{save_path}'。")
|
|
except IOError as e:
|
|
messagebox.showerror("错误", f"写入文件时出错: {e}")
|
|
else:
|
|
messagebox.showinfo("取消", "下载已取消。")
|
|
else:
|
|
messagebox.showinfo("未找到", f"未找到ID为 {image_id} 的图片。")
|
|
except Exception as e:
|
|
messagebox.showerror("下载错误", f"下载图片时发生错误: {e}")
|
|
finally:
|
|
connection.close()
|
|
|
|
def view_all_images_info():
|
|
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
# 查询所有图片信息
|
|
cursor.execute("SELECT id, filename FROM images")
|
|
image_data = cursor.fetchall()
|
|
|
|
if image_data:
|
|
# 构建信息消息
|
|
message = "全部图片信息:\n\n"
|
|
for row in image_data:
|
|
message += f"Image ID: {row[0]}\nImage Name: {row[1]}\n---\n"
|
|
|
|
messagebox.showinfo("所有图片信息", message)
|
|
else:
|
|
messagebox.showinfo("未找到图片", "数据库中没有图片信息。")
|
|
except Exception as e:
|
|
messagebox.showerror("查询错误", f"Error fetching image info: {e}")
|
|
finally:
|
|
connection.close()
|
|
|