简体   繁体   中英

sqlite3 python3, user input for database

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM