-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
46 lines (41 loc) · 1.4 KB
/
database.py
File metadata and controls
46 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import time
import mysql.connector
from mysql.connector import Error
def get_db_connection(retries=5, delay=3):
for attempt in range(retries):
try:
return mysql.connector.connect(
host="mysql",
port=3306,
user="root",
password="",
database="savvylingua"
)
except Error as e:
print(f"Attempt {attempt+1} failed: {e}")
time.sleep(delay)
raise Exception("Could not connect to the database after several attempts.")
def initialize_tables():
db = get_db_connection()
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS Documents (
DocumentID INT AUTO_INCREMENT PRIMARY KEY,
FileName VARCHAR(255),
Language VARCHAR(50) DEFAULT 'Korean',
UploadDate DATETIME DEFAULT CURRENT_TIMESTAMP,
IsPublic BOOLEAN DEFAULT TRUE
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS ExtractedTexts (
TextID INT AUTO_INCREMENT PRIMARY KEY,
DocumentID INT,
OriginalText LONGTEXT,
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (DocumentID) REFERENCES Documents(DocumentID) ON DELETE CASCADE
)
""")
db.commit()
cursor.close()
db.close()