TonyMo

3_7_CRUD.py

Apr 21st, 2021
905
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #  Was create_card_complete.py from 103 week 3 7
  2.  
  3. """
  4. The CRUD model sets out the four basic functions which
  5. allow you to work with data:
  6. .    Create: inserting new data
  7. .    Read: retrieving data
  8. .    Update: updating data which is already stored
  9. .    Delete: removing data
  10. """
  11.  
  12. import sqlite3
  13. conn = sqlite3.connect("computer_cards.db")
  14.  
  15.  
  16. # Def Create ; ok Works
  17. def create(name, cores, cpu_speed, ram, cost):
  18.     insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) "\
  19.                  " VALUES ('{}', {}, {}, {}, {})" .format(name, cores, cpu_speed, ram, cost)
  20.  
  21.     conn.execute(insert_sql)
  22.  
  23.     conn.commit()
  24.  
  25.  
  26. # Def Read  ; ok Read Works
  27. def read(name):
  28.     select_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
  29.  
  30.     result = conn.execute(select_sql)
  31.  
  32.     return result.fetchone()
  33.  
  34.  
  35. # Def Update
  36. def update(name, cores, cpu_speed, ram, cost):
  37.     update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} "\
  38.                  "WHERE name = '{}'".format(cores, cpu_speed, ram, cost, name)
  39.  
  40.     conn.execute(update_sql)
  41.  
  42.     conn.commit()
  43.  
  44.  
  45. # Def Delete
  46. def delete(name):
  47.     delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
  48.  
  49.     conn.execute(delete_sql)
  50.  
  51.     conn.commit()
  52.  
  53.  
  54. command = input("(C)reate, (R)ead, (U)pdate or (D)elete a card ")
  55.  
  56. if command == "C":
  57.     name = input("Computer Name >")
  58.     cores = input("Number of Cores >")
  59.     cpu_speed = input("CPU speed (GHz) >")
  60.     ram = input("RAM (MB) >")
  61.     cost = input("Cost ($) >")
  62.  
  63.     create(name, cores, cpu_speed, ram, cost)
  64.  
  65. elif command == "R":
  66.     name = input("Computer Name >")
  67.  
  68.     card = read(name)
  69.  
  70.     print(card)
  71.    
  72. elif command == "U":
  73.     name = input("Computer Name >")
  74.     cores = input("Cores >")
  75.     cpu_speed = input("CPU speed (GHz) >")
  76.     ram = input("RAM (MB) >")
  77.     cost = input("Cost ($) >")
  78.  
  79.     update(name, cores, cpu_speed, ram, cost)
  80.    
  81. elif command == "D":
  82.     name = input("Computer Name >")
  83.    
  84.     card = delete(name)
  85.  
  86. conn.close()
  87.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×