This repository was archived by the owner on Sep 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathokul_db_deneme.py
More file actions
197 lines (173 loc) · 6.27 KB
/
Copy pathokul_db_deneme.py
File metadata and controls
197 lines (173 loc) · 6.27 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import psycopg2
class Okul:
def __init__(self, ogrenciler=None, personel=None, branslar=None, id=None):
self.ogrenciler = ogrenciler
self.personel = personel
self.branslar = branslar
self.id = id
class Ogrenciler(Okul):
def __init__(self, id, ad, soyad, adres, tel, brans):
super().__init__()
self.id = id
self.ad = ad
self.soyad = soyad
self.adres = adres
self.tel = tel
self.brans = brans
class Personel(Okul):
def __init__(self, per_id, per_ad, per_soyad, per_adres, per_tel, per_brans):
super().__init__()
self.per_id = per_id
self.per_ad = per_ad
self.per_soyad = per_soyad
self.per_adres = per_adres
self.per_tel = per_tel
self.per_brans = per_brans
class Otomasyon:
def __init__(self):
self.baglanti_ayarlari = {
"dbname": "ogrenci_db",
"user": "postgres",
"password": "asd",
"host": "localhost",
"port": "5432",
}
self.connection = None # Initialize connection to None
self.cursor = None # Initialize cursor to None
self.baglanti_olustur()
def baglanti_olustur(self):
try:
self.connection = psycopg2.connect(**self.baglanti_ayarlari)
self.cursor = self.connection.cursor()
self.tablo_olustur()
print("Bağlantı başarılı")
except psycopg2.Error as e:
print("Bağlantı hatası: ", e)
def tablo_olustur(self):
try:
# Check if the cursor has been initialized
if self.cursor is not None:
self.cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ogrenciler(
id SERIAL PRIMARY KEY,
ad VARCHAR(50),
soyad VARCHAR(50),
adres VARCHAR(100),
tel VARCHAR(15),
brans VARCHAR(50)
);
CREATE TABLE IF NOT EXISTS personeller(
per_id SERIAL PRIMARY KEY,
per_ad VARCHAR(50),
per_soyad VARCHAR(50),
per_adres VARCHAR(100),
per_tel VARCHAR(15),
per_brans VARCHAR(50)
);
CREATE TABLE IF NOT EXISTS branslar(
br_id SERIAL PRIMARY KEY,
brans VARCHAR(50)
);
"""
)
self.connection.commit()
else:
print("Cursor not initialized. Check database connection.")
except psycopg2.Error as e:
print("Tablo oluşturma hatası: ", e)
def ogrenci_ekle(self, ad, soyad, adres, tel, brans):
try:
# Check if the cursor has been initialized
if self.cursor is not None:
self.cursor.execute(
"INSERT INTO ogrenciler (ad, soyad, adres, tel, brans) VALUES (%s, %s, %s, %s, %s)",
(ad, soyad, adres, tel, brans),
)
self.connection.commit()
print("Öğrenci eklendi.")
else:
print("Cursor not initialized. Check database connection.")
except psycopg2.Error as e:
print("Öğrenci ekleme hatası: ", e)
def ogrenci_sil(self, id):
try:
# Check if the cursor has been initialized
if self.cursor is not None:
self.cursor.execute("DELETE FROM ogrenciler WHERE id = %s", (id,))
self.connection.commit()
print("Öğrenci silindi.")
else:
print("Cursor not initialized. Check database connection.")
except psycopg2.Error as e:
print("Öğrenci silme hatası: ", e)
def ogrencileri_listele(self):
try:
# Check if the cursor has been initialized
if self.cursor is not None:
self.cursor.execute("SELECT * FROM ogrenciler")
for ogrenci in self.cursor.fetchall():
print(ogrenci)
else:
print("Cursor not initialized. Check database connection.")
except psycopg2.Error as e:
print("Öğrencileri listeleme hatası: ", e)
otomasyon = Otomasyon()
def menu(secim):
match secim:
case "1":
ad = input("Ad: ")
soyad = input("Soyad: ")
adres = input("Adres: ")
tel = input("Tel: ")
brans = input("Branş: ")
otomasyon.ogrenci_ekle(ad, soyad, adres, tel, brans)
case "2":
id = int(input("Silmek istediğiniz öğrencinin id'si: "))
otomasyon.ogrenci_sil(id)
case "3":
otomasyon.ogrencileri_listele()
case "0":
print("Çıkış yapılıyor...")
return False
case _:
print("Hatalı seçim yaptınız...")
return True
while True:
print("""A = Öğrenci Otomasyonu
B = Personel Otomasyonu
C = Branş Otomasyonu
0 = Çıkış""")
secim = input("Bir seçenek giriniz: ")
if secim == "0":
print("Programdan çıkılıyor...")
break
if secim == "A":
print("\nÖğrenci Otomasyonu")
print("1. Öğrenci Ekle")
print("2. Öğrenci Sil")
print("3. Öğrencileri Listele")
print("0. Çıkış")
alt_secim = input("Seçiminizi yapınız: ")
if not menu(alt_secim):
break
elif secim == "B":
print("\nPersonel Otomasyonu")
print("1. Personel Ekle")
print("2. Personel Sil")
print("3. Personel Listele")
print("0. Çıkış")
alt_secim = input("Seçiminizi yapınız: ")
if not menu(alt_secim):
break
elif secim == "C":
print("\nBranş Otomasyonu")
print("1. Branş Ekle")
print("2. Branş Sil")
print("3. Branş Listele")
print("0. Çıkış")
alt_secim = input("Seçiminizi yapınız: ")
if not menu(alt_secim):
break
else:
print("Geçersiz seçim. Lütfen tekrar deneyin.")