|
|
@@ -1,4 +1,6 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+from unicodedata import category
|
|
|
+
|
|
|
import mysql.connector
|
|
|
from mysql.connector import Error
|
|
|
|
|
|
@@ -45,31 +47,23 @@ def initialize_files_database():
|
|
|
connection.close()
|
|
|
print("MySQL 服务器连接已关闭")
|
|
|
|
|
|
-# 初始化表
|
|
|
-def initialize_files_table(table_name=None):
|
|
|
- if not table_name:
|
|
|
- table_name = 'filePath'
|
|
|
-
|
|
|
- # 验证表名是否合法(这里的规则可以根据你的需求做调整)
|
|
|
- allowed_tables = ['fruit', 'filePath'] # 你可以在这里列出允许的表名
|
|
|
- if table_name not in allowed_tables:
|
|
|
- raise ValueError(f"不允许使用此表名:{table_name}")
|
|
|
-
|
|
|
+def initialize_files_table(table_name = "category"):
|
|
|
try:
|
|
|
connection = mysql.connector.connect(**DB_CONFIG)
|
|
|
if connection.is_connected():
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
- # 动态创建表
|
|
|
+ # 动态创建表,指定字符集为 utf8mb4
|
|
|
create_table_query = f'''
|
|
|
CREATE TABLE IF NOT EXISTS `{table_name}` (
|
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
- name VARCHAR(100)
|
|
|
- )
|
|
|
+ name VARCHAR(255) UNIQUE,
|
|
|
+ url VARCHAR(255) UNIQUE
|
|
|
+ ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
|
|
|
'''
|
|
|
|
|
|
cursor.execute(create_table_query)
|
|
|
- print(f"--- 创建 {table_name} 表 ---")
|
|
|
+ print(f"表 {table_name} 初始化成功。")
|
|
|
except Error as err:
|
|
|
print(f"初始化表时发生错误:{err}")
|
|
|
finally:
|
|
|
@@ -78,56 +72,58 @@ def initialize_files_table(table_name=None):
|
|
|
connection.close()
|
|
|
print("MySQL 连接已关闭")
|
|
|
|
|
|
+
|
|
|
# 插入数据
|
|
|
-def insert_url(name):
|
|
|
+def insert_url(name, url, table_name="category"):
|
|
|
try:
|
|
|
connection = mysql.connector.connect(**DB_CONFIG)
|
|
|
if connection.is_connected():
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
- # 插入数据
|
|
|
- cursor.execute("INSERT INTO filePath (name) VALUES (%s)", (name,))
|
|
|
+ # 确保目标文件表存在
|
|
|
+ initialize_files_table(table_name)
|
|
|
+
|
|
|
+ # 插入文件数据到目标表
|
|
|
+ insert_query = f"INSERT INTO `{table_name}` (name, url) VALUES (%s, %s)"
|
|
|
+ cursor.execute(insert_query, (name, url))
|
|
|
connection.commit()
|
|
|
- print(f"插入数据成功:({name})")
|
|
|
- except Error as err:
|
|
|
- print(f"插入数据时发生错误:{err}")
|
|
|
- finally:
|
|
|
- if connection.is_connected():
|
|
|
- cursor.close()
|
|
|
- connection.close()
|
|
|
- print("MySQL 连接已关闭")
|
|
|
|
|
|
-# 插入数据
|
|
|
-def insert_data(name, origin):
|
|
|
- try:
|
|
|
- connection = mysql.connector.connect(**DB_CONFIG)
|
|
|
- if connection.is_connected():
|
|
|
- cursor = connection.cursor()
|
|
|
+ # 判断插入是否成功
|
|
|
+ if cursor.rowcount > 0:
|
|
|
+ print(f"插入文件数据成功:({name}, {url}) 到表 {table_name}")
|
|
|
+ file_result = {"status": "success"}
|
|
|
+ else:
|
|
|
+ print(f"插入文件数据失败:({name}, {url}) 到表 {table_name}")
|
|
|
+ file_result = {"status": "failed"}
|
|
|
|
|
|
- # 插入数据
|
|
|
- cursor.execute("INSERT INTO filePath (name, origin) VALUES (%s, %s)", (name, origin))
|
|
|
- connection.commit()
|
|
|
- print(f"插入数据成功:({name}, {origin})")
|
|
|
- except Error as err:
|
|
|
- print(f"插入数据时发生错误:{err}")
|
|
|
- finally:
|
|
|
- if connection.is_connected():
|
|
|
- cursor.close()
|
|
|
- connection.close()
|
|
|
- print("MySQL 连接已关闭")
|
|
|
+ # 确保 category 表存在
|
|
|
+ initialize_files_table("category")
|
|
|
|
|
|
-def delete_data(name):
|
|
|
- try:
|
|
|
- connection = mysql.connector.connect(**DB_CONFIG)
|
|
|
- if connection.is_connected():
|
|
|
- cursor = connection.cursor()
|
|
|
+ # 查询是否已经存在该表名
|
|
|
+ select_query = "SELECT * FROM `category` WHERE `name` = %s"
|
|
|
+ cursor.execute(select_query, (table_name,))
|
|
|
+ existing_category = cursor.fetchone()
|
|
|
+
|
|
|
+ if existing_category:
|
|
|
+ print(f"表名 {table_name} 已经存在,不插入!")
|
|
|
+ category_result = {"status": "exists"}
|
|
|
+ else:
|
|
|
+ # 执行插入 category
|
|
|
+ insert_query = "INSERT INTO `category` (name) VALUES (%s)"
|
|
|
+ cursor.execute(insert_query, (table_name,))
|
|
|
+ connection.commit()
|
|
|
+ print(f"插入表名成功:({table_name}) 到表 category")
|
|
|
+ category_result = {"status": "success"}
|
|
|
+
|
|
|
+ return {"status": "success", "file_result": file_result, "category_result": category_result}
|
|
|
+
|
|
|
+ except mysql.connector.Error as err:
|
|
|
+ if err.errno == 1062: # Duplicate entry error
|
|
|
+ print(f"文件 {name} 已存在,不重复插入。")
|
|
|
+ return {"status": "duplicate", "message": f"文件 {name} 已存在"}
|
|
|
+ print(f"插入数据时发生错误:{err}")
|
|
|
+ return {"status": "error", "message": str(err)}
|
|
|
|
|
|
- # 删除数据
|
|
|
- cursor.execute("DELETE FROM filePath WHERE name = %s", (name,))
|
|
|
- connection.commit()
|
|
|
- print(f"成功删除名称为 {name} 的记录")
|
|
|
- except Error as err:
|
|
|
- print(f"删除数据时发生错误:{err}")
|
|
|
finally:
|
|
|
if connection.is_connected():
|
|
|
cursor.close()
|
|
|
@@ -135,17 +131,19 @@ def delete_data(name):
|
|
|
print("MySQL 连接已关闭")
|
|
|
|
|
|
|
|
|
+
|
|
|
# 查询数据
|
|
|
-def fetch_data():
|
|
|
+def fetch_data(table_name):
|
|
|
try:
|
|
|
connection = mysql.connector.connect(**DB_CONFIG)
|
|
|
if connection.is_connected():
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
# 查询数据
|
|
|
- cursor.execute("SELECT * FROM filePath")
|
|
|
+ query = f"SELECT * FROM `{table_name}`"
|
|
|
+ cursor.execute(query)
|
|
|
rows = cursor.fetchall()
|
|
|
- print("--- 查询结果 ---")
|
|
|
+ print(f"--- 表 {table_name} 的查询结果 ---")
|
|
|
for row in rows:
|
|
|
print(row)
|
|
|
return rows
|
|
|
@@ -159,13 +157,10 @@ def fetch_data():
|
|
|
|
|
|
# 主函数
|
|
|
if __name__ == "__main__":
|
|
|
- # 初始化数据库和表
|
|
|
+ # 初始化数据库
|
|
|
initialize_files_database()
|
|
|
- initialize_files_table()
|
|
|
-
|
|
|
- # 插入示例数据
|
|
|
- insert_data('Apple', 'China')
|
|
|
- insert_data('Banana', 'Ecuador')
|
|
|
|
|
|
- # 查询数据
|
|
|
- fetch_data()
|
|
|
+ # 示例:插入和查询数据
|
|
|
+ table_name = "filePath"
|
|
|
+ insert_url("example_file.mp4", table_name)
|
|
|
+ fetch_data(table_name)
|