yusufgundogdu commited on
Commit
d0b4807
·
verified ·
1 Parent(s): aa4c97e

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +16 -15
database.py CHANGED
@@ -1,34 +1,33 @@
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
21
 
22
  def close_db(e=None):
 
23
  db = g.pop('db', None)
24
  if db is not None:
25
  db.close()
26
 
27
  def init_db(app):
 
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,
@@ -37,9 +36,11 @@ def init_db(app):
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()
 
1
  import sqlite3
2
+ import os
3
  from flask import g
4
 
5
+ # Veritabanı dosya yolu
6
+ DB_NAME = 'stablediffusionapi.db'
7
+ DB_PATH = os.path.join(os.path.dirname(__file__), DB_NAME)
 
 
 
 
8
 
9
  def get_db():
10
+ """Veritabanı bağlantısını oluşturur"""
11
  if 'db' not in g:
 
12
  # Dizin yoksa oluştur
13
+ os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
14
+ g.db = sqlite3.connect(DB_PATH)
15
+ g.db.row_factory = sqlite3.Row # Sözlük benzeri erişim
16
  return g.db
17
 
18
  def close_db(e=None):
19
+ """Bağlantıyı kapatır"""
20
  db = g.pop('db', None)
21
  if db is not None:
22
  db.close()
23
 
24
  def init_db(app):
25
+ """Veritabanını başlatır ve örnek verileri ekler"""
26
  with app.app_context():
27
  db = get_db()
28
  cursor = db.cursor()
29
 
30
+ # Kullanıcılar tablosu
31
  cursor.execute('''
32
  CREATE TABLE IF NOT EXISTS users (
33
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
36
  )
37
  ''')
38
 
39
+ # Örnek veriler (sadece tablo boşsa)
40
  cursor.execute('SELECT COUNT(*) FROM users')
41
  if cursor.fetchone()[0] == 0:
42
+ cursor.executemany(
43
+ 'INSERT INTO users (name, score) VALUES (?, ?)',
44
+ [('Ahmet', 85), ('Mehmet', 92)]
45
+ )
46
  db.commit()