Update database.py
Browse files- database.py +29 -14
database.py
CHANGED
|
@@ -1,23 +1,38 @@
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def create_connection():
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def create_table():
|
| 13 |
conn = create_connection()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Veritabanı dosyasının oluşturulacağı dizin
|
| 5 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
+
DB_NAME = "database.db"
|
| 7 |
+
DB_PATH = os.path.join(BASE_DIR, DB_NAME)
|
| 8 |
|
| 9 |
def create_connection():
|
| 10 |
+
try:
|
| 11 |
+
# Veritabanı dosyasının bulunduğu dizin mevcut değilse oluştur
|
| 12 |
+
if not os.path.exists(BASE_DIR):
|
| 13 |
+
os.makedirs(BASE_DIR)
|
| 14 |
+
conn = sqlite3.connect(DB_PATH)
|
| 15 |
+
return conn
|
| 16 |
+
except sqlite3.OperationalError as e:
|
| 17 |
+
print(f"Veritabanı bağlantısı oluşturulamadı: {e}")
|
| 18 |
+
return None
|
| 19 |
|
| 20 |
def create_table():
|
| 21 |
conn = create_connection()
|
| 22 |
+
if conn is not None:
|
| 23 |
+
try:
|
| 24 |
+
cursor = conn.cursor()
|
| 25 |
+
cursor.execute('''
|
| 26 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 27 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 28 |
+
name TEXT NOT NULL,
|
| 29 |
+
email TEXT NOT NULL UNIQUE
|
| 30 |
+
)
|
| 31 |
+
''')
|
| 32 |
+
conn.commit()
|
| 33 |
+
except sqlite3.Error as e:
|
| 34 |
+
print(f"Tablo oluşturulurken hata oluştu: {e}")
|
| 35 |
+
finally:
|
| 36 |
+
conn.close()
|
| 37 |
+
else:
|
| 38 |
+
print("Veritabanı bağlantısı kurulamadığı için tablo oluşturulamadı.")
|