|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import pymysql.cursors
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
from tkinter import filedialog
|
|
|
|
|
from tkinter import filedialog,messagebox
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# 数据库连接参数
|
|
|
|
@ -45,42 +45,37 @@ def upload_image():
|
|
|
|
|
sql = "INSERT INTO images (filename, content) VALUES (%s, %s)"
|
|
|
|
|
cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content)))
|
|
|
|
|
connection.commit()
|
|
|
|
|
print(f"Image '{selected_image_path}' uploaded successfully.")
|
|
|
|
|
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("SELECT * FROM images WHERE id = %s", (image_id,))
|
|
|
|
|
image_data = cursor.fetchone()
|
|
|
|
|
if image_data:
|
|
|
|
|
# 输出图像信息
|
|
|
|
|
print(f"Image Details:")
|
|
|
|
|
for index, value in enumerate(image_data):
|
|
|
|
|
column_name = cursor.description[index][0]
|
|
|
|
|
print(f"{column_name}: {value}")
|
|
|
|
|
# 提示用户确认删除
|
|
|
|
|
confirmation = tk.messagebox.askyesno("Confirm Delete", f"是否删除图片ID为 {image_id}的图片?")
|
|
|
|
|
if confirmation:
|
|
|
|
|
# 执行删除操作
|
|
|
|
|
sql = "DELETE FROM images WHERE id = %s"
|
|
|
|
|
cursor.execute(sql, (image_id,))
|
|
|
|
|
connection.commit()
|
|
|
|
|
print(f"成功删除图片ID为 {image_id} 的图片.")
|
|
|
|
|
# 直接执行删除操作
|
|
|
|
|
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:
|
|
|
|
|
print(f"没有发现图片ID为 {image_id}的图片.")
|
|
|
|
|
messagebox.showinfo("操作取消", "图片删除操作已取消。")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error deleting image: {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:
|
|
|
|
@ -100,18 +95,37 @@ def download_image():
|
|
|
|
|
try:
|
|
|
|
|
with open(save_path, 'wb') as image_file:
|
|
|
|
|
image_file.write(content)
|
|
|
|
|
print(f"Image downloaded successfully to '{save_path}'.")
|
|
|
|
|
messagebox.showinfo("成功", f"图片已成功下载到 '{save_path}'。")
|
|
|
|
|
except IOError as e:
|
|
|
|
|
print(f"Error writing to file: {e}")
|
|
|
|
|
messagebox.showerror("错误", f"写入文件时出错: {e}")
|
|
|
|
|
else:
|
|
|
|
|
print("Download cancelled.")
|
|
|
|
|
messagebox.showinfo("取消", "下载已取消。")
|
|
|
|
|
else:
|
|
|
|
|
print(f"No image found with ID {image_id}.")
|
|
|
|
|
messagebox.showinfo("未找到", f"未找到ID为 {image_id} 的图片。")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error downloading image: {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()
|
|
|
|
|
|
|
|
|
|