· 4 years ago · Aug 25, 2021, 10:24 AM
1from os import curdir
2import sqlite3
3
4class Register():
5
6 def __init__(self):
7 self.setupDB()
8
9
10 def register(self):
11 email = input('Enter your student email: ')
12 age = int(input('Enter your current age: '))
13 con = self.getCursor()
14 cursor = con.cursor()
15
16
17 query = "INSERT INTO students VALUES (?, ?)"
18 cursor.execute(query, (email, age))
19 con.commit()
20 cursor.close()
21
22
23 def retriveEntries(self):
24 query = """SELECT students.email, students.age FROM students"""
25 cursor = self.getCursor().cursor()
26
27 cursor.execute(query)
28 results = cursor.fetchall()
29 print(results)
30 cursor.close()
31
32 def setupDB(self):
33
34 query = """CREATE TABLE IF NOT EXISTS students(
35 email TEXT,
36 age INTEGER
37 )"""
38 con = self.getCursor()
39 cursor = con.cursor()
40 cursor.execute(query)
41 con.commit()
42 cursor.close()
43
44 def getCursor(self):
45
46
47 try:
48 con = sqlite3.connect('testdb.db')
49 # cursor = con.cursor()
50 return con
51 except sqlite3.Error as error:
52 print(f'Error occured: {error}')
53 self.getCursor()
54
55
56
57
58
59Reg = Register()
60
61Reg.retriveEntries()
62
63