-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Learner.py
More file actions
104 lines (77 loc) · 2.87 KB
/
MySQL_Learner.py
File metadata and controls
104 lines (77 loc) · 2.87 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import os
import sqlite3
import logging
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import Qt
from ui.main_window import MainWindow
# ============================================================
# EXE-SAFE RESOURCE PATH
# ============================================================
def resource_path(relative_path: str) -> str:
"""
Return absolute path – works for PyInstaller EXE and normal Python.
"""
if hasattr(sys, "_MEIPASS"):
base = sys._MEIPASS
else:
base = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base, relative_path)
# ============================================================
# SAFE LOGGING LOCATION (LOCALAPPDATA)
# ============================================================
def setup_logging():
local_appdata = os.getenv("LOCALAPPDATA", "")
log_dir = os.path.join(local_appdata, "MySQL_Learner")
os.makedirs(log_dir, exist_ok=True)
log_path = os.path.join(log_dir, "mysql_learner.log")
logging.basicConfig(
filename=log_path,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("Application started.")
logging.info(f"Logging to: {log_path}")
# ============================================================
# DATABASE INITIALIZATION (SQLite)
# ============================================================
def ensure_database_exists():
"""
Ensures /database/sample.db exists.
Creates it using /database/init.sql if missing.
"""
# Where the EXE/runtime is located
base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
db_dir = os.path.join(base_dir, "database")
db_path = os.path.join(db_dir, "sample.db")
init_sql_path = resource_path("database/init.sql")
os.makedirs(db_dir, exist_ok=True)
if os.path.exists(db_path):
logging.info(f"Database found: {db_path}")
return
logging.info("Database missing — creating new SQLite DB...")
logging.info(f"Using init SQL at: {init_sql_path}")
try:
with open(init_sql_path, "r", encoding="utf-8") as f:
script = f.read()
conn = sqlite3.connect(db_path)
cur = conn.cursor()
cur.executescript(script)
conn.commit()
conn.close()
logging.info(f"Database created successfully at: {db_path}")
except Exception as e:
logging.error(f"Database creation error: {e}")
# ============================================================
# MAIN APPLICATION ENTRY
# ============================================================
if __name__ == "__main__":
app = QApplication(sys.argv)
setup_logging()
ensure_database_exists()
window = MainWindow(app_name="MySQL Learner")
# IMPORTANT:
# Do NOT maximize here.
# main_window.py will control geometry and maximization.
window.show()
sys.exit(app.exec())