lundi 4 avril 2016

How to find the presence of value in my database before adding to it?

I am new to SQLite Database so I can't figure what is the problem, I am willing to create a database for the list of cars and their rates present in the world.

    import sqlite3
db = sqlite3.connect('Car.db')
cur= db.cursor()
var = (int)(input("How many cars you want to enter?"))
for i in range(var):
    Has = input('CAR :')
    for name in Has:
        cur.execute('SELECT count(*) FROM CARS WHERE Name = ?',(name,))
        base = cur.fetchone()[0]
        if base ==0:
            print('CAR EXISTS')
        else:
            some = input('RATE:')
            cur.execute('''INSERT INTO CARS(Name,Rate)VALUES(?,?)''',{'Name':Has,'Rate':some})
db.close()

I want to check whether the entered car is already present before adding to my database but when I do so it shows the following output

CAR: Audi CAR EXISTS CAR EXISTS CAR EXISTS CAR EXISTS

Thank you

Aucun commentaire:

Enregistrer un commentaire