stablediffusionapi / database.py
yusufgundogdu's picture
Update database.py
cb0f8d2 verified
raw
history blame
1.21 kB
import sqlite3
import os
from flask import g
def get_db_path():
# Docker'da /data dizinine yazma garantisi veriyoruz
if os.environ.get('DOCKER_ENV') == 'true':
return '/data/database.db'
return 'database.db' # Local için proje dizini
def get_db():
if 'db' not in g:
db_path = get_db_path()
# Dizin yoksa oluştur
os.makedirs(os.path.dirname(db_path), exist_ok=True)
g.db = sqlite3.connect(db_path)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_db(app):
with app.app_context():
db = get_db()
cursor = db.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
score INTEGER DEFAULT 0
)
''')
cursor.execute('SELECT COUNT(*) FROM users')
if cursor.fetchone()[0] == 0:
cursor.executemany('INSERT INTO users (name, score) VALUES (?, ?)',
[('Ahmet', 85), ('Mehmet', 92)])
db.commit()