-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths13
124 lines (98 loc) · 3.99 KB
/
s13
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
class DoesNotExist(Exception):
pass
class MultipleObjectsReturned(Exception):
pass
class InvalidField(Exception):
pass
class Student:
def __init__(self,name, age, score):
self.name = name
self.student_id = None
self.age = age
self.score = score
def __repr__(self):
return "Student(student_id={0}, name={1}, age={2}, score={3})".format(self.student_id,self.name,self.age,self.score)
def delete(self):
sql_query='delete from student where student_id={}'.format(self.student_id)
write_data(sql_query)
def save(self):
if self.student_id is None:
query="insert into student(name,age,score) values ('{}',{},{})".format(self.name,self.age,self.score)
write_data(query)
q1='select student_id from student where name="{}" and age={} and score={}'.format(self.name,self.age,self.score)
a=read_data(q1)
self.student_id=a[0][0]
else:
sql_query="update student set student_id={},name='{}',age={},score={} where student_id={}".format(self.student_id,self.name,self.age,self.score,self.b)
write_data(sql_query)
@classmethod
def get(cls,**kid):
for x,y in kid.items():
cls.a=x
cls.b=y
if str(x) not in ('name','age','score','student_id'):
raise InvalidField
query="select * from student where {} = '{}'".format(cls.a,cls.b)
obj=read_data(query)
if len(obj)>1:
raise MultipleObjectsReturned
elif len(obj)==0:
raise DoesNotExist
elif len(obj)==1:
c=Student(obj[0][1],obj[0][2],obj[0][3])
c.student_id=obj[0][0]
return c
@classmethod
def filter(cls,**kid):
cls.li=[]
cls.operator={'lt':'<','lte':'<=','gt':'>','gte':'>=','neq':'!=','in':'in','contain':''}
if(len(kid))>=1:
l=[]
for x,y in kid.items():
cls.a=x
cls.b=y
field=cls.a
field=field.split('__')
if field[0] not in ('name','age','score','student_id'):
raise InvalidField
if(len(field))==1:
query=" {}='{}'".format(cls.a,cls.b)
elif field[1]=='contains':
query=" {} like '%{}%'".format(field[0],cls.b)
elif field[1]=='in':
query=" {} {} {}".format(field[0],cls.operator[field[1]],tuple(cls.b))
else:
query="{} {} '{}'".format(field[0],cls.operator[field[1]],cls.b)
l.append(query)
x = " and ".join(tuple(l))
query= "select * from student where "+x
#print(query)
obj=read_data(query)
for i in range(len(obj)):
c=Student(obj[i][1],obj[i][2],obj[i][3])
c.student_id=obj[i][0]
cls.li.append(c)
return cls.li
def write_data(sql_query):
import sqlite3
connection = sqlite3.connect("selected_students.sqlite3")
crsr = connection.cursor()
crsr.execute("PRAGMA foreign_keys=on;")
crsr.execute(sql_query)
connection.commit()
connection.close()
def read_data(sql_query):
import sqlite3
connection = sqlite3.connect("selected_students.sqlite3")
crsr = connection.cursor()
crsr.execute(sql_query)
ans= crsr.fetchall()
connection.close()
return ans
#selected_students = Student.filter(age__lte=30)
#print(selected_students)
#selected_students = Student.filter(age__contains="Maude Vanhorne")
#print(selected_students)
#selected_students = Student.filter(age=15,name="Jesse Couch")
#print(selected_students)
#selected_students = Student.filter(age__in = [25, 34], score__lt=50)