Advertisement
Antypas

crud model

Apr 29th, 2020
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. #The CRUD (Create, Read, Update, Delete) model
  2. '''
  3. (1) Create: inserting new data
  4. (2) Read: retrieving data
  5. (3) Update: updating data which is already stored
  6. (4) Delete: removing data
  7. '''
  8. #Create a Python program which can insert (create), read, update, or delete rows in the computer table
  9.  
  10. import sqlite3
  11. conn = sqlite3.connect("computer_cards.db")
  12.  
  13. #(1) Create
  14. #To insert a new row into a table you will need to use
  15. #an INSERT SQL statement. The syntax looks like:
  16.  
  17. #SQL syntax >>> INSERT INTO table(field_a, field_b) VALUES (value_a, value_b)
  18.  
  19. #Create a function which, when passed the name of a computer
  20. #and the number of cores, will construct a SQL INSERT statement
  21. #and execute it.
  22. def create(name, cores, cpu_speed, ram, cost):
  23.     insert_sql = "INSERT INTO computer(name,cores,cpu_speed,ram,cost) VALUES ('{}',{},{},{},{})".format(name,cores,cpu_speed,ram,cost)
  24.     #note use of .format method above; {} are called markers
  25.     conn.execute(insert_sql)
  26.  
  27.     conn.commit()
  28.  
  29. #(2) Read
  30. #SQL syntax >>> SELECT * from computer WHERE name = 'name of computer'
  31. def read(name):        # name of desired computer
  32.     select_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
  33.  
  34.     result = conn.execute(select_sql)
  35.  
  36.     return result.fetchone()         # note method .fetchone() instead of .fetchall()
  37.  
  38. '''Tip:
  39. If more than one row is returned, you can still use fetchone().
  40. When called multiple times it returns the next row in order
  41. until there are no more rows left, when it returns None.
  42. This can be useful for iterating through rows.
  43. '''
  44.  
  45. #(3) Update
  46. #SQL syntax >>> UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'
  47. def update (name, cores, cpu_speed, ram, cost):
  48.     update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'".format(cores,cpu_speed,ram,cost,name)
  49.    
  50.     result = conn.execute(update_sql)
  51.    
  52.     conn.commit()
  53.  
  54. #(4) Delete
  55. #SQL syntax >>> DELETE FROM computer WHERE name = '{}'
  56. def delete(name):
  57.     delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
  58.    
  59.     result = conn.execute(delete_sql)
  60.    
  61.     conn.commit()
  62.  
  63. #user interface
  64. command = input("(C)reate or (R)ead or (U)pdate or (D)elete a card > ")
  65. name = input("Name >")
  66. if command == "C":
  67. #    name = input("Name >")
  68.     cores = input("Cores >")
  69.     cpu_speed = input("CPU speed (GHz) >")
  70.     ram = input("RAM (MB) >")
  71.     cost = input("Cost ($) >")
  72.  
  73.     create(name, cores, cpu_speed, ram, cost)
  74.  
  75. elif command == "R":
  76. #    name = input("Name >")
  77.  
  78.     card = read(name)
  79.  
  80.     print(card)
  81.  
  82. elif command == "U":
  83. #    name = input("Name >")
  84.     if read(name) == None:
  85.         print(name + " does not exist in database -> cannot update")
  86.     else:
  87.         update(name, cores, cpu_speed, ram, cost)
  88.    
  89. elif command == "D":
  90. #    name = input("Name >")
  91.     if read(name) == None:
  92.         print(name + " does not exist in database -> cannot delete")
  93.     else:
  94.         delete(name)
  95.    
  96.     if read(name) == None:
  97.         print(name + " successfully deleted from database")
  98.     else:
  99.         print("why is " + name + " still here?")
  100.  
  101. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement