Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Create a Python program which can insert, update, or delete rows in the computer table
- #To insert a new row into a table you will need to use
- #an INSERT SQL statement. The syntax looks like:
- # INSERT INTO table(field_a, field_b) VALUES (value_a, value_b)
- import sqlite3
- conn = sqlite3.connect("computer_cards.db")
- #Create a function which, when passed the name of a computer
- #and the number of cores, will construct a SQL INSERT statement
- #and execute it.
- 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)
- #note use of .format method above; {} are called markers
- conn.execute(insert_sql)
- conn.commit()
- #create("My computer", 4)
- #add a user interface to the program to gather the name and number of cores
- #so you can use it to add multiple computers:
- print("Enter the details:")
- name = input("Name > ")
- cores = input("Cores > ")
- cpu_speed = input("CPU_speed > ")
- ram = input("RAM > ")
- cost = input("Cost > ")
- create(name, cores, cpu_speed, ram, cost)
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement