Advertisement
timber101

PICardsDataBaseAdmin

Jan 14th, 2021
957
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None
  1.  
  2. # PI Cards Database Admin
  3. import sqlite3 # get the module
  4.  
  5. conn = sqlite3.connect("computer_cards.db") #connect to the databse
  6.  
  7. #create function to add new cards
  8. def createnew(name, cores, cpu_speed, ram, cost):
  9.     insert_sql = "insert into computer(name, cores, cpu_speed, ram, cost) values ('{}', {}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
  10.     conn.execute(insert_sql)
  11.     conn.commit()
  12.  
  13. #read funtion to bring back a card    
  14. def read(name):
  15.     select_sql = "select * from computer where name = '{}'".format(name)
  16.     result = conn.execute(select_sql)
  17.    
  18.     return result.fetchone()
  19.    
  20. #update a record
  21. def update(name, cores, cpu_speed, ram, cost):
  22.     update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}' ".format(cores, cpu_speed,ram,cost,name)
  23.     conn.execute(update_sql)
  24.     conn.commit()
  25.     print("record updated")
  26.    
  27. #delete a record
  28. def deleterecord(name):
  29.     del_sql = "DELETE from computer WHERE name = '{}'".format(name)
  30.     conn.execute(del_sql)
  31.     conn.commit()
  32.     print("Record deleted")
  33.    
  34. # menu
  35. print("Pi Cards Database")
  36. print("*"* 17 )
  37. command = ""
  38. while command != "Q":
  39.     command = input("(C)reate, (R)ead, (U)pdate, (D)elete a record or Q to quit>> ")
  40.  
  41.     if command.upper() == "C":
  42.         print("Create a new card")
  43.         entname = input("Name >> ")
  44.         entcores = input("Cores >> ")
  45.         entcpu_speed = input("CPU speed >> ")
  46.         entram = input ("RAM >> ")
  47.         entcost = input ("Cost >> ")
  48.        
  49.         try:
  50.             createnew(entname, entcores, entcpu_speed, entram, entcost)
  51.             print("Entry added")
  52.         except:
  53.             print("Sorry dude something went wrong")
  54.            
  55.     elif command.upper() == "R":
  56.         print("Return a record")
  57.  
  58.         entname = input("Name >> ")
  59.         rtncard = read(entname)
  60.         print(rtncard)
  61.        
  62.        
  63.     elif command.upper() == "U":
  64.         print("Update a record")
  65.        
  66.         tgtname = input("Name you want to update >> ")
  67.         print("Reading current record")
  68.         rtncard = read(tgtname)
  69.         print(rtncard)
  70.        
  71.         print("Update record, press enter to skip")
  72.         entname = tgtname
  73.         entcores = input("Update Cores >> ")
  74.         if entcores == "":
  75.             entcores=rtncard[1]
  76.         entcpu_speed = input("Update CPU speed >> ")
  77.         if entcpu_speed == "":
  78.             entcores=rtncard[2]
  79.         entram = input ("Update RAM >> ")
  80.         if entram == "":
  81.             entram=rtncard[3]
  82.         entcost = input ("Update Cost >> ")
  83.         if entcores == "":
  84.             entcost=rtncard[4]
  85.         update(entname, entcores, entcpu_speed, entram, entcost)
  86.         rtncard = read(tgtname)
  87.         print("Record update to >> ", rtncard)
  88.        
  89.     elif command.upper() == "D":
  90.         print("Delete a record")
  91.        
  92.         tgtname = input("Name you want to delete >> ")
  93.         print("Reading current record")
  94.         rtncard = read(tgtname)
  95.         print(rtncard)
  96.        
  97.         answer = input("Are you sure you want to delete this record Y/N ?")
  98.         if answer.upper() =="Y":
  99.             deleterecord(tgtname)
  100.         else:
  101.             print(f"You entered {answer}, no action taken only 'Y' will remove a record")
  102.    
  103.     elif command.upper() =="Q":
  104.         break
  105.    
  106. print("Thats all folkes")
  107.        
  108.    
  109.  
  110.  
  111.    
Advertisement
RAW Paste Data Copied
Advertisement