Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import libraries
- import sqlite3
- #connect to database
- conn = sqlite3.connect("computer_cards.db")
- #define computer data creation function
- 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)
- conn.commit()
- #define computer data update function
- def update(name, cores, cpu_speed, ram, cost):
- update_sql = "UPDATE computer SET cores = {}, cpu_speed = {}, ram = {}, cost = {} WHERE name = '{}'".format(cores, cpu_speed, ram, cost, name)
- conn.execute(update_sql)
- conn.commit()
- #define database read function
- def read(name):
- select_sql = "SELECT * FROM computer WHERE name = '{}'".format(name)
- result = conn.execute(select_sql)
- return result.fetchone()
- #define database delete function
- def delete(name):
- delete_sql = "DELETE FROM computer WHERE name = '{}'".format(name)
- conn.execute(delete_sql)
- conn.commit()
- #choose whether to create, update, delete or read via database
- command = input("(C)reate, (U)pdate, (D)elete or (R)ead a card: ")
- 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)
- if 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)
- elif command == "R":
- name = input("Name >")
- card = read(name)
- print(card)
- elif command == "D":
- name = input("Name >")
- delete(name)
- #close database connection
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement