Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PI Cards Database Admin
- import sqlite3 # get the module
- conn = sqlite3.connect("computer_cards.db") #connect to the databse
- #create function to add new cards
- def createnew(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()
- #read funtion to bring back a card
- def read(name):
- select_sql = "select * from computer where name = '{}'".format(name)
- result = conn.execute(select_sql)
- return result.fetchone()
- #update a record
- 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()
- print("record updated")
- #delete a record
- def deleterecord(name):
- del_sql = "DELETE from computer WHERE name = '{}'".format(name)
- conn.execute(del_sql)
- conn.commit()
- print("Record deleted")
- # menu
- print("Pi Cards Database")
- print("*"* 17 )
- command = ""
- while command != "Q":
- command = input("(C)reate, (R)ead, (U)pdate, (D)elete a record or Q to quit>> ")
- if command.upper() == "C":
- print("Create a new card")
- entname = input("Name >> ")
- entcores = input("Cores >> ")
- entcpu_speed = input("CPU speed >> ")
- entram = input ("RAM >> ")
- entcost = input ("Cost >> ")
- try:
- createnew(entname, entcores, entcpu_speed, entram, entcost)
- print("Entry added")
- except:
- print("Sorry dude something went wrong")
- elif command.upper() == "R":
- print("Return a record")
- entname = input("Name >> ")
- rtncard = read(entname)
- print(rtncard)
- elif command.upper() == "U":
- print("Update a record")
- tgtname = input("Name you want to update >> ")
- print("Reading current record")
- rtncard = read(tgtname)
- print(rtncard)
- print("Update record, press enter to skip")
- entname = tgtname
- entcores = input("Update Cores >> ")
- if entcores == "":
- entcores=rtncard[1]
- entcpu_speed = input("Update CPU speed >> ")
- if entcpu_speed == "":
- entcores=rtncard[2]
- entram = input ("Update RAM >> ")
- if entram == "":
- entram=rtncard[3]
- entcost = input ("Update Cost >> ")
- if entcores == "":
- entcost=rtncard[4]
- update(entname, entcores, entcpu_speed, entram, entcost)
- rtncard = read(tgtname)
- print("Record update to >> ", rtncard)
- elif command.upper() == "D":
- print("Delete a record")
- tgtname = input("Name you want to delete >> ")
- print("Reading current record")
- rtncard = read(tgtname)
- print(rtncard)
- answer = input("Are you sure you want to delete this record Y/N ?")
- if answer.upper() =="Y":
- deleterecord(tgtname)
- else:
- print(f"You entered {answer}, no action taken only 'Y' will remove a record")
- elif command.upper() =="Q":
- break
- print("Thats all folkes")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement