-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_db.py
More file actions
163 lines (119 loc) · 5.24 KB
/
memory_db.py
File metadata and controls
163 lines (119 loc) · 5.24 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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 17:01:13 2020
@author: Dogancan Torun
"""
#executescript soru işaretleriyle kullanılamaz
import sqlite3
conn = None
try:
conn = sqlite3.connect('student3.sqlite')
cur = conn.cursor()
cur.executescript("""
CREATE TABLE IF NOT EXISTS student(student_no INTEGER PRIMARY KEY, student_name VARCHAR(64), school_id INTEGER);
CREATE TABLE IF NOT EXISTS school(school_id INTEGER PRIMARY KEY AUTOINCREMENT, school_name VARCHAR(64));
""")
conn.commit()
no = int(input('Öğrencinin numarasını giriniz:'))
student_name = input('Öğrencinin adını ve soyadını giriniz:')
school_name = input('Okulun adını ve soyadını giriniz:')
cur.execute("INSERT INTO school(school_name) VALUES(?)", (school_name,))
cur.execute("INSERT INTO student(student_no, student_name, school_id) VALUES(?, ?, (SELECT school_id FROM school WHERE school_name = ?))", (no, student_name, school_name))
conn.commit()
except sqlite3.Error as e:
print('Error', e)
finally:
if conn:
conn.close()
#yukarıdaki kodun revize edilmiş hali
import sqlite3
conn = None
try:
conn = sqlite3.connect('student3.sqlite')
cur = conn.cursor()
cur.executescript("""
CREATE TABLE IF NOT EXISTS student(student_no INTEGER PRIMARY KEY, student_name VARCHAR(64), school_id INTEGER);
CREATE TABLE IF NOT EXISTS school(school_id INTEGER PRIMARY KEY AUTOINCREMENT, school_name VARCHAR(64), UNIQUE(school_name));
""")
conn.commit()
no = int(input('Öğrencinin numarasını giriniz:'))
student_name = input('Öğrencinin adını ve soyadını giriniz:')
school_name = input('Okulun adını giriniz:')
cur.execute("INSERT OR IGNORE INTO school(school_name) VALUES(?)", (school_name,))
cur.execute("INSERT INTO student(student_no, student_name, school_id) VALUES(?, ?, (SELECT school_id FROM school WHERE school_name = ?))", (no, student_name, school_name))
conn.commit()
except sqlite3.Error as e:
print('Error', e)
finally:
if conn:
conn.close()
#memorydatabase örneği
import sqlite3
conn = None
try:
conn = sqlite3.connect(':memory:') #memory db yptık
cur = conn.cursor()
cur.executescript("""
CREATE TABLE IF NOT EXISTS student(student_no INTEGER PRIMARY KEY, student_name VARCHAR(64), school_id INTEGER);
CREATE TABLE IF NOT EXISTS school(school_id INTEGER PRIMARY KEY AUTOINCREMENT, school_name VARCHAR(64), UNIQUE(school_name));
""")
conn.commit()
while True:
no = int(input('Öğrencinin numarasını giriniz:'))
if not no:
break
student_name = input('Öğrencinin adını ve soyadını giriniz:')
school_name = input('Okulun adını giriniz:')
cur.execute("INSERT OR IGNORE INTO school(school_name) VALUES(?)", (school_name,))
cur.execute("INSERT INTO student(student_no, student_name, school_id) VALUES(?, ?, (SELECT school_id FROM school WHERE school_name = ?))", (no, student_name, school_name))
conn.commit()
cur.execute("SELECT student.student_no, student.student_name, school.school_name FROM student, school WHERE student.school_id = school.school_id")
for no, student_name, school_name in cur:
print(f'{no}, {student_name}, {school_name}')
except sqlite3.Error as e:
print('Error', e)
finally:
if conn:
conn.close()
#memorydatabase örneği
import sqlite3
conn = None
try:
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute("CREATE TABLE student(student_no INTEGER, student_name VARCHAR(64), student_bdate DATE)")
cur.execute("INSERT INTO student VALUES(123, 'Hasan Bal', '2005-12-23')")
cur.execute("INSERT INTO student VALUES(456, 'Sadık Dursun', '2004-11-22')")
cur.execute("INSERT INTO student VALUES(768, 'Ayşe Er', '2003-08-17')")
conn.commit()
cur.execute("SELECT * FROM student")
for no, name, bdate in cur:
print(no, name, bdate, sep=', ')
except sqlite3.Error as e:
print('Error', e)
finally:
if conn:
conn.close()
#database date uygulaması
import sqlite3
conn = None
try:
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute("CREATE TABLE student(student_no INTEGER, student_name VARCHAR(64), student_bdate DATE)")
cur.execute("INSERT INTO student VALUES(123, 'Hasan Bal', '2005-12-23')")
cur.execute("INSERT INTO student VALUES(456, 'Sadık Dursun', '2004-11-22')")
cur.execute("INSERT INTO student VALUES(768, 'Ayşe Er', '2003-08-17')")
conn.commit()
cur.execute("SELECT * FROM student")
import datetime
days = ['Pazartesi', 'Salı', 'Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi', 'Pazar']
for no, name, bdate in cur:
date = datetime.date.fromisoformat(bdate)
print(no, name, date, date, days[date.weekday()], sep=', ')
except sqlite3.Error as e:
print('Error', e)
finally:
if conn:
conn.close()
#connection nesnesinin icinde text factory ve row factory var.Bu fonksiyonarın kullanımını bize kolayıklar sağlıyor