Advertisement
Antypas

Creating new cards

Apr 28th, 2020
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #Create a Python program which can insert, update, or delete rows in the computer table
  2.  
  3. #To insert a new row into a table you will need to use
  4. #an INSERT SQL statement. The syntax looks like:
  5.  
  6. # INSERT INTO table(field_a, field_b) VALUES (value_a, value_b)
  7.  
  8. import sqlite3
  9. conn = sqlite3.connect("computer_cards.db")
  10.  
  11. #Create a function which, when passed the name of a computer
  12. #and the number of cores, will construct a SQL INSERT statement
  13. #and execute it.
  14. def create(name, cores, cpu_speed, ram, cost):
  15.     insert_sql = "INSERT INTO computer(name,cores,cpu_speed,ram,cost) VALUES ('{}',{},{},{},{})".format(name,cores,cpu_speed,ram,cost)
  16.     #note use of .format method above; {} are called markers
  17.     conn.execute(insert_sql)
  18.  
  19.     conn.commit()
  20.  
  21. #create("My computer", 4)
  22.  
  23.  
  24. #add a user interface to the program to gather the name and number of cores
  25. #so you can use it to add multiple computers:
  26. print("Enter the details:")
  27.  
  28. name = input("Name > ")
  29. cores = input("Cores > ")
  30. cpu_speed = input("CPU_speed > ")
  31. ram = input("RAM > ")
  32. cost = input("Cost > ")
  33.  
  34. create(name, cores, cpu_speed, ram, cost)
  35.  
  36. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement