-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheSQLiteHandshake.py
More file actions
42 lines (34 loc) · 1.38 KB
/
TheSQLiteHandshake.py
File metadata and controls
42 lines (34 loc) · 1.38 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
import sqlite3
db_connection = sqlite3.connect('school.db')
my_cursor = db_connection.cursor()
my_cursor.execute('CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT, grade INTEGER)')
def add_student(name, grade):
my_cursor.execute("INSERT INTO students (name, grade) VALUES (?, ?)", (name, grade)) # parametized query
db_connection.commit()
def print_students():
my_cursor.execute("SELECT * FROM students")
rows = my_cursor.fetchall()
if not rows: print("There are no students yet!")
else:
for row in rows: print(row)
def print_above_fifty():
my_cursor.execute("SELECT * FROM students WHERE grade > 50")
my_rows = my_cursor.fetchall()
if not my_rows: print("There are no students yet!")
else:
for row in my_rows: print(row)
user_response = input("Enter another student? (y/n): ")
while user_response == 'y':
student_name = input("Student Name: ")
try:
student_grade = int(input("Student Grade: "))
except Exception as e:
print(f"There was a {type(e).__name__} error!")
print(f"Error description: {e}")
add_student(student_name, student_grade)
user_response = input("Enter another student? (y/n): ")
else:
print("\n~~~~~~~~~~~~~~~~~~~ALL STUDENTS~~~~~~~~~~~~~~~~~~")
print_students()
print("\n~~~~~~~~~~~~~~~~~~~ABOVE 50~~~~~~~~~~~~~~~~~~~~~~~")
print_above_fifty()