Advertisement
GamerBhai02

APP Exp 7 Assignment

Apr 17th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | Source Code | 0 0
  1. import sqlite3
  2.  
  3. def connect_db():
  4.     db = sqlite3.connect("studentdb.db")
  5.     print("Database connected")
  6.     return db
  7.  
  8. def create_table(db):
  9.     cursor = db.cursor()
  10.     cursor.execute("create table if not exists student(name text,usn text primary key,sem integer,marks integer)")
  11.     print("Table created")
  12.     db.commit()
  13.  
  14. def insert_data(db,name,usn,sem,marks):
  15.     cursor = db.cursor()
  16.     cursor.execute("insert into student(name,usn,sem,marks) values(?,?,?,?)",(name,usn,sem,marks))
  17.     db.commit()
  18.  
  19. def fetch_data(db):
  20.     cursor = db.cursor()
  21.     # cursor.execute("select * from users")
  22.     # rows = cursor.fetchall()
  23.     print("Fetching Data...")
  24.     for i in cursor.execute("select * from student"):
  25.         print(i)
  26.     db.commit()
  27.  
  28. def update_data(db,usn,marks):
  29.     cursor = db.cursor()
  30.     cursor.execute("update student set marks=? where usn=?",(marks,usn))
  31.     print("\nTable updated")
  32.     db.commit()
  33.  
  34. def delete_data(db,usn):
  35.     cursor = db.cursor()
  36.     cursor.execute("delete from student where usn=?",(usn,))
  37.     print("\n1 Data deleted")
  38.     db.commit()
  39.  
  40. def drop_table(db):
  41.     cursor = db.cursor()
  42.     cursor.execute("drop table student")
  43.     print("\nTable dropped")
  44.     db.commit()
  45.    
  46. def marks_display(db,marks):
  47.     cursor = db.cursor()
  48.     print("\nFetching custom data...")
  49.     for i in cursor.execute("select * from student where marks>?",(marks,)):
  50.         print(i)
  51.     db.commit()
  52.  
  53. if __name__=="__main__":
  54.     db = connect_db()
  55.     create_table(db)
  56.     insert_data(db,"Talha","1NH23AI006",4,45)
  57.     insert_data(db,"Aneesh","1NH23AI017",4,40)
  58.     insert_data(db,"Rohith","1NH23AI001",4,20)
  59.     insert_data(db,"Anil","1NH23AI019",4,30)
  60.     print("\nAll Data inserted")
  61.     fetch_data(db)
  62.     update_data(db,"1NH23AI001",23)
  63.     fetch_data(db)
  64.     delete_data(db,"1NH23AI019")
  65.     fetch_data(db)
  66.     marks_display(db,25)
  67.     drop_table(db)
  68.     db.close()
  69.     print("Database disconnected")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement