Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from datetime import datetime
- conn = sqlite3.connect("demo.db")
- c = conn.cursor()
- c.execute("CREATE TABLE IF NOT EXISTS studenttb(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER,dt TEXT)")
- conn.commit()
- def insert():
- name = input("Name: ")
- age = int(input("Age: "))
- dt = datetime.now()
- c.execute("INSERT INTO studenttb(name,age,dt) VALUES (?,?,?)",(name,age,dt))
- conn.commit()
- def display():
- rows = c.execute("SELECT * FROM studenttb").fetchall()
- for r in rows: print(r)
- def update():
- id = int(input("ID: "))
- new_age = int(input("New age: "))
- dt = datetime.now()
- c.execute("UPDATE studenttb SET age=?,dt=? WHERE id=?",(new_age,dt,id))
- conn.commit()
- def delete():
- id = int(input("ID: "))
- c.execute("DELETE FROM studenttb WHERE id=?", (id,))
- conn.commit()
- while True:
- choice = input("1-Insert 2-Display 3-Update 4-Delete 5-Exit: ")
- if choice=="1": insert()
- elif choice=="2": display()
- elif choice=="3": update()
- elif choice=="4": delete()
- elif choice=="5": break
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment