import sqlite3
conn = sqlite3.connect("demo.db")
cursor = conn.cursor()
cursor.execute(
''' CREATE TABLE IF NOT EXISTS studenttb(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER) '''
)
conn.commit()
def insert():
name = input("Enter the name : ")
age = int(input("Enter the age : "))
cursor.execute('''INSERT INTO studenttb(name,age) VALUES (?,?)''',(name,age))
conn.commit()
print("Record Inserted")
def display():
cursor.execute(''' SELECT * FROM studenttb ''')
row = cursor.fetchall()
for r in row:
print(r)
def update():
id = int(input("Enter the id to update : "))
new_age = int(input("Enter the new age : "))
cursor.execute("UPDATE studenttb SET age=? WHERE id=?",(new_age,id))
conn.commit()
print("Record Updated")
def delete():
id = int(input("Enter the id to delete : "))
cursor.execute("DELETE FROM studenttb WHERE id=?",(id,))
print("Record Deleted")
while True:
print("1 - Insert")
print("2 - Display")
print("3 - Update")
print("4 - Delete")
print("5 - Exit")
choice = input("Enter the choice : ")
if choice == "1":
insert()
elif choice == "2":
display()
elif choice == "3":
update()
elif choice == "4":
delete()
elif choice == "5":
print("Exit")
break
else:
print("Wrong choice")
conn.close()