scipiguy

Futurelearn Files - Computer DB update & delete added

Nov 27th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. import sqlite3
  2. conn = sqlite3.connect("computer_cards.db")
  3.  
  4. def create(name, cores, cpu_speed, ram, cost):
  5.     insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}',{}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
  6.     conn.execute(insert_sql)
  7.     conn.commit()
  8.  
  9. def read(name):
  10.     select_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
  11.     result = conn.execute(select_sql)
  12.     return result.fetchone()
  13.  
  14. def update(name):
  15.     update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'"
  16.     conn.execute(update_sql)
  17.     conn.commit()
  18.  
  19. def delete(name):
  20.     delete_sql = "DELETE FROM computer WHERE name = '{}'"
  21.     conn.execute(delete_sql)
  22.     conn.commit()
  23.  
  24. exitsystem = False
  25.  
  26. while exitsystem == False:
  27.  
  28.     command = input("(C)reate, (R)ead, (U)pdate, (D)elete a card or (Q)uit? > ")
  29.  
  30.     if command == "C":
  31.         print("Enter the details to create computer:")
  32.         name = input("Name > ")
  33.         cores = input("Cores > ")
  34.         cpu_speed = input("CPU Speed(GHz) > ")
  35.         ram = input("RAM(MB) > ")
  36.         cost = input("Cost($) > ")
  37.         create(name, cores, cpu_speed, ram, cost)
  38.     elif command == "R":
  39.         print("Enter the details to read:")
  40.         name = input("Name > ")
  41.         card = read(name)
  42.         print(card)
  43.     elif command == "U":
  44.         print("Enter the computer's details to update computer:")
  45.         name = input("Name > ")
  46.         cores = input("Cores > ")
  47.         cpu_speed = input("CPU Speed(GHz) > ")
  48.         ram = input("RAM(MB) > ")
  49.         cost = input("Cost($) > ")
  50.         update(name, cores, cpu_speed, ram, cost)
  51.     elif command == "D":
  52.         print("Enter the computer's details to delete:")
  53.         name = input("Name > ")
  54.         confirm = input("Are you sure you want to delete the record for " + name + "? (Y\\N) > " )
  55.         if confirm == "Y":
  56.             print(name, "has been removed from the database.")
  57.             delete(name)
  58.         else:
  59.             print("OK. No records have been deleted.")
  60.     elif command == "Q":
  61.         print("Goodbye")
  62.         exitsystem = True
  63.  
  64. conn.close()
Add Comment
Please, Sign In to add comment