Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,32 @@ auth = HTTPBasicAuth()
|
|
| 11 |
|
| 12 |
# Hàm kết nối với SQLite
|
| 13 |
def get_db_connection():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Tạo các bảng nếu chưa tồn tại
|
| 19 |
def init_db():
|
|
|
|
| 11 |
|
| 12 |
# Hàm kết nối với SQLite
|
| 13 |
def get_db_connection():
|
| 14 |
+
db_path = '/app/database.db'
|
| 15 |
+
db_dir = os.path.dirname(db_path)
|
| 16 |
+
|
| 17 |
+
# Tạo thư mục /app/ nếu chưa tồn tại
|
| 18 |
+
if not os.path.exists(db_dir):
|
| 19 |
+
try:
|
| 20 |
+
os.makedirs(db_dir, exist_ok=True)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
raise RuntimeError(f"Không thể tạo thư mục {db_dir}: {str(e)}")
|
| 23 |
+
|
| 24 |
+
# Kiểm tra quyền ghi
|
| 25 |
+
if not os.access(db_dir, os.W_OK):
|
| 26 |
+
# Nếu không có quyền ghi, thử sử dụng /tmp/
|
| 27 |
+
db_path = '/tmp/database.db'
|
| 28 |
+
db_dir = os.path.dirname(db_path)
|
| 29 |
+
if not os.path.exists(db_dir):
|
| 30 |
+
os.makedirs(db_dir, exist_ok=True)
|
| 31 |
+
if not os.access(db_dir, os.W_OK):
|
| 32 |
+
raise RuntimeError(f"Không có quyền ghi tại {db_dir} và /tmp/")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
conn = sqlite3.connect(db_path)
|
| 36 |
+
conn.row_factory = sqlite3.Row # Để trả về dữ liệu dưới dạng dictionary
|
| 37 |
+
return conn
|
| 38 |
+
except sqlite3.OperationalError as e:
|
| 39 |
+
raise RuntimeError(f"Không thể mở file cơ sở dữ liệu tại {db_path}: {str(e)}")
|
| 40 |
|
| 41 |
# Tạo các bảng nếu chưa tồn tại
|
| 42 |
def init_db():
|