lexlepty commited on
Commit
94ae66d
·
verified ·
1 Parent(s): fad8627

Update models.py

Browse files
Files changed (1) hide show
  1. models.py +44 -44
models.py CHANGED
@@ -1,45 +1,45 @@
1
- import pymysql
2
- from config import Config
3
-
4
- class Database:
5
- def __init__(self):
6
- self.conn = pymysql.connect(
7
- host=Config.MYSQL_CONFIG['host'],
8
- port=Config.MYSQL_CONFIG['port'],
9
- user=Config.MYSQL_CONFIG['user'],
10
- password=Config.MYSQL_CONFIG['password'],
11
- charset='utf8mb4',
12
- cursorclass=pymysql.cursors.DictCursor
13
- )
14
- self.init_db()
15
-
16
- def init_db(self):
17
- with self.conn.cursor() as cursor:
18
- # 创建数据库
19
- cursor.execute(f"CREATE DATABASE IF NOT EXISTS {Config.MYSQL_CONFIG['database']}")
20
- cursor.execute(f"USE {Config.MYSQL_CONFIG['database']}")
21
-
22
- # 创建文件表
23
- cursor.execute("""
24
- CREATE TABLE IF NOT EXISTS files (
25
- id BIGINT PRIMARY KEY AUTO_INCREMENT,
26
- original_name VARCHAR(255) NOT NULL,
27
- stored_name VARCHAR(255) NOT NULL,
28
- file_path VARCHAR(500) NOT NULL,
29
- file_type VARCHAR(50),
30
- file_size BIGINT,
31
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32
- UNIQUE KEY idx_path (file_path)
33
- ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
34
- """)
35
- self.conn.commit()
36
-
37
- def search_files(self, keyword):
38
- with self.conn.cursor() as cursor:
39
- cursor.execute("""
40
- SELECT * FROM files
41
- WHERE original_name LIKE %s
42
- OR file_path LIKE %s
43
- ORDER BY created_at DESC
44
- """, (f"%{keyword}%", f"%{keyword}%"))
45
  return cursor.fetchall()
 
1
+ import pymysql
2
+ from config import Config
3
+
4
+ class Database:
5
+ def __init__(self):
6
+ self.conn = pymysql.connect(
7
+ host=Config.MYSQL_CONFIG['host'],
8
+ port=Config.MYSQL_CONFIG['port'],
9
+ user=Config.MYSQL_CONFIG['user'],
10
+ password=Config.MYSQL_CONFIG['password'],
11
+ charset='utf8mb4',
12
+ cursorclass=pymysql.cursors.DictCursor
13
+ )
14
+ self.init_db()
15
+
16
+ def init_db(self):
17
+ with self.conn.cursor() as cursor:
18
+ # 创建数据库
19
+ cursor.execute(f"CREATE DATABASE IF NOT EXISTS {Config.MYSQL_CONFIG['database']}")
20
+ cursor.execute(f"USE {Config.MYSQL_CONFIG['database']}")
21
+
22
+ # 创建文件表
23
+ cursor.execute("""
24
+ CREATE TABLE IF NOT EXISTS files (
25
+ id BIGINT PRIMARY KEY AUTO_INCREMENT,
26
+ original_name VARCHAR(255) NOT NULL,
27
+ stored_name VARCHAR(255) NOT NULL,
28
+ file_path VARCHAR(500) NOT NULL,
29
+ file_type VARCHAR(50),
30
+ file_size BIGINT,
31
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32
+ UNIQUE KEY idx_path (file_path)
33
+ ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
34
+ """)
35
+ self.conn.commit()
36
+
37
+ def search_files(self, keyword):
38
+ with self.conn.cursor() as cursor:
39
+ cursor.execute("""
40
+ SELECT * FROM files
41
+ WHERE original_name LIKE %s
42
+ OR file_path LIKE %s
43
+ ORDER BY created_at DESC
44
+ """, (f"%{keyword}%", f"%{keyword}%"))
45
  return cursor.fetchall()