Update database.py
Browse files- database.py +12 -2
database.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
import sqlite3
|
| 2 |
-
import os
|
| 3 |
from flask import g
|
| 4 |
|
| 5 |
def get_db_path():
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def get_db():
|
| 9 |
if 'db' not in g:
|
| 10 |
db_path = get_db_path()
|
|
|
|
|
|
|
| 11 |
g.db = sqlite3.connect(db_path)
|
| 12 |
g.db.row_factory = sqlite3.Row
|
| 13 |
return g.db
|
|
@@ -21,6 +28,7 @@ def init_db(app):
|
|
| 21 |
with app.app_context():
|
| 22 |
db = get_db()
|
| 23 |
cursor = db.cursor()
|
|
|
|
| 24 |
cursor.execute('''
|
| 25 |
CREATE TABLE IF NOT EXISTS users (
|
| 26 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -28,8 +36,10 @@ def init_db(app):
|
|
| 28 |
score INTEGER DEFAULT 0
|
| 29 |
)
|
| 30 |
''')
|
|
|
|
| 31 |
cursor.execute('SELECT COUNT(*) FROM users')
|
| 32 |
if cursor.fetchone()[0] == 0:
|
| 33 |
cursor.executemany('INSERT INTO users (name, score) VALUES (?, ?)',
|
| 34 |
[('Ahmet', 85), ('Mehmet', 92)])
|
|
|
|
| 35 |
db.commit()
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
+
import os # Eksik olan import eklendi
|
| 3 |
from flask import g
|
| 4 |
|
| 5 |
def get_db_path():
|
| 6 |
+
# Hugging Face Spaces için özel yol kontrolü
|
| 7 |
+
if os.path.exists('/data'):
|
| 8 |
+
return '/data/users.db'
|
| 9 |
+
elif os.path.exists('/home/user_data'):
|
| 10 |
+
return '/home/user_data/users.db'
|
| 11 |
+
return 'users.db'
|
| 12 |
|
| 13 |
def get_db():
|
| 14 |
if 'db' not in g:
|
| 15 |
db_path = get_db_path()
|
| 16 |
+
# Dizin yoksa oluştur
|
| 17 |
+
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
| 18 |
g.db = sqlite3.connect(db_path)
|
| 19 |
g.db.row_factory = sqlite3.Row
|
| 20 |
return g.db
|
|
|
|
| 28 |
with app.app_context():
|
| 29 |
db = get_db()
|
| 30 |
cursor = db.cursor()
|
| 31 |
+
|
| 32 |
cursor.execute('''
|
| 33 |
CREATE TABLE IF NOT EXISTS users (
|
| 34 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 36 |
score INTEGER DEFAULT 0
|
| 37 |
)
|
| 38 |
''')
|
| 39 |
+
|
| 40 |
cursor.execute('SELECT COUNT(*) FROM users')
|
| 41 |
if cursor.fetchone()[0] == 0:
|
| 42 |
cursor.executemany('INSERT INTO users (name, score) VALUES (?, ?)',
|
| 43 |
[('Ahmet', 85), ('Mehmet', 92)])
|
| 44 |
+
|
| 45 |
db.commit()
|