maroph icon

db_insert.py

maroph | PRO | 11/16/19 10:45:34 AM UTC | 0 ⭐ | 376 👁️ | Never ⏰ | []
Python |

1.38 KB

|

None

|

0 👍

/

0 👎

import sqlite3
 
 
def get_input_data(name):
    first = True
    resp = ''
    while resp == '':
        if not first:
            print("data missing please enter the data")
        resp = input("{0:10}: ".format(name))
        resp = resp.strip()
        first = False
    return resp
 
 
def create(connection, name, cores, cpu_speed, ram, cost):
    # field names/types of table computer
    # name      : text
    # cores     : integer
    # cpu_speed : real
    # ram       : real
    # cost      : real
 
    insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}', {}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
    # print("SQL:>>>>>" + insert_sql + "<<<<<")
 
    connection.execute(insert_sql)
    connection.commit()
 
 
conn = sqlite3.connect("computer_cards.db")
 
resp = "Y"
while resp in ["Y","y",""]:
    print("")
    print("Enter the details:")
    name = get_input_data("Name")
    cores = get_input_data("Cores")
    cpu_speed = get_input_data("CPU speed")
    ram = get_input_data("RAM")
    cost = get_input_data("Price")
 
    create(conn, name, cores, cpu_speed, ram, cost)
    resp = input("Add another card?[y/n]: ")
 
print("")
print("Dump of table computer:")
result = conn.execute("SELECT * FROM computer")
computers = result.fetchall()
 
for computer in computers:
    print(computer)
 
conn.close()

Comments