Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3 #This imports the "sqlite3" lib
- chand = sqlite3.connect("computer_cards.db") #This sets a connectivity handler to the d/B
- #First define your functions
- #This creates a new function called "create_new" with 5 parameters
- def create_record(name, cores, cpu_speed, ram, cost):
- insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}', {}, {},{},{})".format(name, cores, cpu_speed, ram, cost)
- #The format method then replaces the {} markers in the SQL statement with the values from the 5 variables.
- chand.execute(insert_sql)
- #The code then needs to execute the insert_sql statement and commit the changes to the database
- chand.commit()
- #This functions reads a record from the database
- def read_record(name):
- read_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
- result = chand.execute(read_sql)
- return result.fetchone()
- #This function deletes a record from the database
- def delete_record(name):
- delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
- chand.execute(delete_sql)
- chand.commit()
- #This function updates a record
- def update_record(name, cores, cpu_speed, ram, cost):
- update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'".format(name, cores, cpu_speed, ram, cost)
- chand.execute(update_sql)
- chand.commit()
- print('********************************')
- print('WELCOME TO THE COMPUTER DATABASE')
- print('********************************')
- print('')
- user_choice = input('(C)reate, (R)ead, (D)elete or (U)pdate a card:> ')
- if user_choice == "C":
- print('')
- print('You choose',user_choice)
- print('')
- print('Please create a new record, with all 5 attributes')
- print('')
- name = input('Name > ')
- cores = input('Cores > ')
- cpu_speed = input('Speed > ')
- ram = input('RAM > ')
- cost = input('Cost > ')
- print('')
- create_record(name, cores, cpu_speed, ram, cost)
- print('You new record is:')
- card = read_record(name)
- print(card)
- elif user_choice == "R":
- print('')
- print('You choose',user_choice)
- print('')
- print('Please enter a computer name for the record you want retrieving')
- print('')
- name = input("Name >")
- print('')
- card = read_record(name)
- print('Here is your chosen record...')
- print(card)
- elif user_choice == 'D':
- print('')
- print('You choose',user_choice)
- print('')
- print('Please enter a computer name for the record you want to delete')
- print('')
- name = input("Name >")
- print('')
- card = delete_record(name)
- elif user_choice == "U":
- print('')
- print('You choose',user_choice)
- print('')
- print('Please update the record which you want to')
- print('')
- name = input('Name > ')
- cores = input('Cores > ')
- cpu_speed = input('Speed > ')
- ram = input('RAM > ')
- cost = input('Cost > ')
- print('')
- update_record(name, cores, cpu_speed, ram, cost)
- else:
- print('')
- print('ERROR. QUITING!')
- quit()
- chand.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement