Advertisement
Fahim999

Untitled

Nov 14th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import sqlite3                                      #This imports the "sqlite3" lib
  2. chand = sqlite3.connect("computer_cards.db")        #This sets a connectivity handler to the d/B
  3.  
  4. #First define your functions
  5.  
  6. #This creates a new function called "create_new" with 5 parameters
  7. def create_record(name, cores, cpu_speed, ram, cost):
  8.     insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}', {}, {},{},{})".format(name, cores, cpu_speed, ram, cost)
  9.     #The format method then replaces the {} markers in the SQL statement with the values from the 5 variables.
  10.     chand.execute(insert_sql)
  11.     #The code then needs to execute the insert_sql statement and commit the changes to the database
  12.     chand.commit()
  13.  
  14. #This functions reads a record from the database
  15. def read_record(name):
  16.     read_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
  17.     result = chand.execute(read_sql)
  18.     return result.fetchone()
  19.  
  20. #This function deletes a record from the database
  21. def delete_record(name):
  22.     delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
  23.     chand.execute(delete_sql)
  24.     chand.commit()
  25.  
  26. #This function updates a record
  27. def update_record(name, cores, cpu_speed, ram, cost):
  28.     update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'".format(name, cores, cpu_speed, ram, cost)
  29.     chand.execute(update_sql)
  30.     chand.commit()
  31.  
  32. print('********************************')
  33. print('WELCOME TO THE COMPUTER DATABASE')
  34. print('********************************')
  35. print('')
  36. user_choice = input('(C)reate, (R)ead, (D)elete or (U)pdate a card:> ')
  37.  
  38. if user_choice == "C":
  39.     print('')
  40.     print('You choose',user_choice)
  41.     print('')
  42.     print('Please create a new record, with all 5 attributes')
  43.     print('')
  44.     name = input('Name > ')
  45.     cores = input('Cores > ')
  46.     cpu_speed = input('Speed > ')
  47.     ram = input('RAM > ')
  48.     cost = input('Cost > ')
  49.     print('')
  50.     create_record(name, cores, cpu_speed, ram, cost)
  51.     print('You new record is:')
  52.     card = read_record(name)
  53.     print(card)
  54.  
  55. elif user_choice == "R":
  56.     print('')
  57.     print('You choose',user_choice)
  58.     print('')
  59.     print('Please enter a computer name for the record you want retrieving')
  60.     print('')
  61.     name = input("Name >")
  62.     print('')
  63.     card = read_record(name)
  64.     print('Here is your chosen record...')
  65.     print(card)
  66.  
  67. elif user_choice == 'D':
  68.     print('')
  69.     print('You choose',user_choice)
  70.     print('')
  71.     print('Please enter a computer name for the record you want to delete')
  72.     print('')
  73.     name = input("Name >")
  74.     print('')
  75.     card = delete_record(name)
  76.  
  77. elif user_choice == "U":
  78.     print('')
  79.     print('You choose',user_choice)
  80.     print('')
  81.     print('Please update the record which you want to')
  82.     print('')
  83.     name = input('Name > ')
  84.     cores = input('Cores > ')
  85.     cpu_speed = input('Speed > ')
  86.     ram = input('RAM > ')
  87.     cost = input('Cost > ')
  88.     print('')
  89.     update_record(name, cores, cpu_speed, ram, cost)
  90.  
  91. else:
  92.     print('')
  93.     print('ERROR. QUITING!')
  94.     quit()
  95. chand.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement