1

I have been trying to enter direct data to an sqlite database from user input but it only captures the first input and leaves out the rest, where could I be wrong?

Here is the code:

import sqlite3 as lite
class DataInput:
    def __init__(self):
        self.id = input("Enter ID: ")
        self.name = input("Enter name: ")
        self.price = input("Enter price: ")
running = True
a = DataInput()
con = lite.connect('kev.db')
with con:
    cur = con.cursor()

    cur.execute("DROP TABLE IF EXISTS cars")

    cur.execute("CREATE TABLE cars(id INT, name TEXT, price INT)")

    cur.execute("INSERT INTO cars VALUES(?, ?, ?)", (a.id, a.name, a.price))
while running:
    DataInput()
    continue
1
  • can you fix the indentation? Can't really understand the code in the current form Commented Feb 27, 2019 at 2:52

1 Answer 1

1

The continue is not helping you.

A constructor that has the side effect of offering three user prompts is, ummm, a bit unusual, but we'll let that one go.

You want to DROP/CREATE once, and then INSERT many times:

with lite.connect('kev.db') as con:
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS cars")
    cur.execute("CREATE TABLE cars(id INT, name TEXT, price INT)")

    running = True
    while running:
        a = DataInput()
        cur.execute("INSERT INTO cars VALUES(?, ?, ?)", (a.id, a.name, a.price))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.