Update database.py
Browse files- database.py +15 -43
database.py
CHANGED
|
@@ -1,46 +1,18 @@
|
|
| 1 |
import sqlite3
|
| 2 |
-
import os
|
| 3 |
-
from flask import g
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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,
|
| 34 |
-
name TEXT NOT NULL,
|
| 35 |
-
score INTEGER DEFAULT 0
|
| 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()
|
|
|
|
| 1 |
import sqlite3
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def create_connection():
|
| 4 |
+
conn = sqlite3.connect("database.db")
|
| 5 |
+
return conn
|
| 6 |
|
| 7 |
+
def create_table():
|
| 8 |
+
conn = create_connection()
|
| 9 |
+
cursor = conn.cursor()
|
| 10 |
+
cursor.execute('''
|
| 11 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 12 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 13 |
+
name TEXT NOT NULL,
|
| 14 |
+
email TEXT NOT NULL UNIQUE
|
| 15 |
+
)
|
| 16 |
+
''')
|
| 17 |
+
conn.commit()
|
| 18 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|