· 4 years ago · Apr 24, 2021, 01:28 PM
1from config import conn
2
3cursor = conn.cursor()
4
5
6def create_table():
7 CREATE_TABLE = """
8 CREATE TABLE IF NOT EXISTS students1(
9 id VARCHAR(40),
10 name VARCHAR(40),
11 age INTEGER,
12 year INTEGER
13 )
14 """
15 cursor.execute(CREATE_TABLE)
16 conn.commit()
17
18def insert_table(id, name, age, year):
19 cursor.execute(f"INSERT INTO students1 VALUES('{id}','{name}', {age}, {year})")
20 conn.commit()
21insert_table("17BD110298", "Derbes", 21, 4)
22
23def select_table():
24 cursor.execute("SELECT * FROM students1")
25 data = cursor.fetchall()
26 return data
27
28def update_table():
29 cursor.execute("UPDATE students1 SET year = 5 WHERE year = 4")
30 conn.commit()
31# create_table()
32update_table()
33
34def delete_from_table():
35 cursor.execute("DELETE FROM students1 WHERE year = 5")
36 conn.commit()
37
38delete_from_table()
39
40print(select_table())