stronk_8s

BASIC SQLITE

Nov 24th, 2025 (edited)
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | Source Code | 0 0
  1. import sqlite3
  2. from datetime import datetime
  3.  
  4. conn = sqlite3.connect("demo.db")
  5. c = conn.cursor()
  6.  
  7. c.execute("CREATE TABLE IF NOT EXISTS studenttb(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER,dt TEXT)")
  8. conn.commit()
  9.  
  10. def insert():
  11.     name = input("Name: ")
  12.     age = int(input("Age: "))
  13.     dt = datetime.now()
  14.     c.execute("INSERT INTO studenttb(name,age,dt) VALUES (?,?,?)",(name,age,dt))
  15.     conn.commit()
  16.  
  17. def display():
  18.     rows = c.execute("SELECT * FROM studenttb").fetchall()
  19.     for r in rows: print(r)
  20.  
  21. def update():
  22.     id = int(input("ID: "))
  23.     new_age = int(input("New age: "))
  24.     dt = datetime.now()
  25.     c.execute("UPDATE studenttb SET age=?,dt=? WHERE id=?",(new_age,dt,id))
  26.     conn.commit()
  27.  
  28. def delete():
  29.     id = int(input("ID: "))
  30.     c.execute("DELETE FROM studenttb WHERE id=?", (id,))
  31.     conn.commit()
  32.  
  33. while True:
  34.     choice = input("1-Insert 2-Display 3-Update 4-Delete 5-Exit: ")
  35.     if choice=="1": insert()
  36.     elif choice=="2": display()
  37.     elif choice=="3": update()
  38.     elif choice=="4": delete()
  39.     elif choice=="5": break
  40.  
  41. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment