File size: 953 Bytes
ff14304 b9521ec 5d57d55 b9521ec 5d57d55 e37b9c9 ff14304 5d57d55 ee6afd3 5d57d55 |
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 |
import sqlite3
import os
from flask import g
def get_db_path():
return 'database.db' # Direkt proje dizininde
def get_db():
if 'db' not in g:
db_path = get_db_path()
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() |