yusufgundogdu commited on
Commit
33d6bf6
·
verified ·
1 Parent(s): b9521ec

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +29 -14
database.py CHANGED
@@ -1,23 +1,38 @@
1
  import sqlite3
2
  import os
3
 
4
- # Veritabanını geçerli çalışma dizinine oluştur
5
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
6
- DB_PATH = os.path.join(BASE_DIR, "database.db")
 
7
 
8
  def create_connection():
9
- conn = sqlite3.connect(DB_PATH)
10
- return conn
 
 
 
 
 
 
 
11
 
12
  def create_table():
13
  conn = create_connection()
14
- cursor = conn.cursor()
15
- cursor.execute('''
16
- CREATE TABLE IF NOT EXISTS users (
17
- id INTEGER PRIMARY KEY AUTOINCREMENT,
18
- name TEXT NOT NULL,
19
- email TEXT NOT NULL UNIQUE
20
- )
21
- ''')
22
- conn.commit()
23
- conn.close()
 
 
 
 
 
 
 
 
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ı.")