maroph

db_insert.py

Nov 16th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import sqlite3
  2.  
  3.  
  4. def get_input_data(name):
  5.     first = True
  6.     resp = ''
  7.     while resp == '':
  8.         if not first:
  9.             print("data missing please enter the data")
  10.         resp = input("{0:10}: ".format(name))
  11.         resp = resp.strip()
  12.         first = False
  13.     return resp
  14.  
  15.  
  16. def create(connection, name, cores, cpu_speed, ram, cost):
  17.     # field names/types of table computer
  18.     # name      : text
  19.     # cores     : integer
  20.     # cpu_speed : real
  21.     # ram       : real
  22.     # cost      : real
  23.  
  24.     insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}', {}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
  25.     # print("SQL:>>>>>" + insert_sql + "<<<<<")
  26.  
  27.     connection.execute(insert_sql)
  28.     connection.commit()
  29.  
  30.  
  31. conn = sqlite3.connect("computer_cards.db")
  32.  
  33. resp = "Y"
  34. while resp in ["Y","y",""]:
  35.     print("")
  36.     print("Enter the details:")
  37.     name = get_input_data("Name")
  38.     cores = get_input_data("Cores")
  39.     cpu_speed = get_input_data("CPU speed")
  40.     ram = get_input_data("RAM")
  41.     cost = get_input_data("Price")
  42.  
  43.     create(conn, name, cores, cpu_speed, ram, cost)
  44.     resp = input("Add another card?[y/n]: ")
  45.  
  46. print("")
  47. print("Dump of table computer:")
  48. result = conn.execute("SELECT * FROM computer")
  49. computers = result.fetchall()
  50.  
  51. for computer in computers:
  52.     print(computer)
  53.  
  54. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment