· 7 years ago · Nov 26, 2018, 09:40 PM
1import sqlite3
2import datetime
3
4
5class Parser:
6 con = sqlite3.connect("Test.db")
7 con.row_factory = sqlite3.Row
8 cur = con.cursor()
9 def __init__(self, path):
10
11 self.read_reqeust(path)
12
13
14 def read_reqeust(self, url):
15 url = url[1:]
16 url = url.split('%3F')
17 keys = url
18 print(keys)
19 if (len(keys) == 1): #GIVE - 20191126_320
20 self.read_database(keys)
21 if (len(keys) > 1):
22 self.write_to_database(keys)
23
24 def write_to_database(self, keys): #GIVE - 320?123456
25 print("zapisuje")
26 x= datetime.datetime.now()
27 data = str(x.year)+str(x.month)+str(x.day)+str(keys[0])
28 sql = "CREATE TABLE IF NOT EXISTS s" +data+ " (id INTEGER PRIMARY KEY ASC, ImieNazwisko varchar(250), Obecnosc BIT DEFAULT 1)"
29 self.cur.execute(sql)
30 imie_nazwisko = "SELECT ImieNaziwsko FROM Studenci WHERE IDLEGIT ="+str(keys[1])+";"
31 self.cur.execute(imie_nazwisko)
32 b = self.cur.fetchone()
33 print(b['ImieNaziwsko'])
34 sql = "INSERT INTO s"+data+" (ImieNazwisko, Obecnosc) VALUES ("+"'"+b['ImieNaziwsko']+"'"+",1);"
35 self.cur.execute(sql)
36 self.con.commit()
37 def read_database(self,keys):
38 lista = []
39 a = self.cur
40 x= datetime.datetime.now()
41 data = str(x.year)+str(x.month)+str(x.day)+str(keys[0])
42 sql = "SELECT ImieNazwisko, Obecnosc FROM s"+ str(data)+" ;" ;
43 a.execute(sql)
44 b = self.cur.fetchall()
45 for c in b:
46 print(c['ImieNazwisko'], c['Obecnosc'])
47 lista.append(str(c['ImieNazwisko'])+"_"+ str(c['Obecnosc']))