Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- conn = sqlite3.connect("computer_cards.db")
- def create(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)
- conn.execute(insert_sql)
- def read(name):
- select_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
- result = conn.execute(select_sql)
- ##NB we use fetchone instead of fetchall see previous Returns single row or None if no data
- return result.fetchone()
- def update(name, cores, cpu_speed,ram,cost):
- card_update = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'".format(cores, cpu_speed, ram, cost, name)
- conn.execute(card_update)
- conn.commit()
- def delete(name):
- delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
- print(delete_sql)
- conn.execute(delete_sql)
- conn.commit()
- while True:
- command = input("(C)reate, (R)ead a card, (U)pdate a card or (D)elete >>> ")
- command=command.upper()
- print(command)
- if command == "C":
- name = input("Name >")
- cores = input("Cores >")
- cpu_speed = input("CPU speed (GHz) >")
- ram = input("RAM (MB) >")
- cost = input("Cost ($) >")
- create(name, cores, cpu_speed, ram, cost)
- card=read(name)
- print('Card created ', card)
- elif command == "R":
- name = input("Name >")
- card = read(name)
- if card is None:
- print('Card not found')
- else:
- print(card)
- elif command == "D":
- name = input("Name >")
- card = read(name)
- if card is None:
- print('Card not found: not deleted')
- else:
- delete(name)
- card = read(name)
- if card is None:
- print('Deleted')
- else: print('Not deleted; duplicate??')
- elif command == "U":
- name = input("Name >")
- cores = input("Cores >")
- cpu_speed = input("CPU speed (GHz) >")
- ram = input("RAM (MB) >")
- cost = input("Cost ($) >")
- update(name, cores, cpu_speed, ram, cost)
- card=read(name)
- print('Card updated ', card)
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement