Search code examples
pythonwindowssqlitepysqlite

Checking duplicate while inserting in SQLite


I am trying to insert a data into SQLite database using Python.

INSERT INTO DATA_TABLE(UID,LABEL) VALUES (NULL, "UK")  
    WHERE "UK" NOT EXISTS IN (SELECT LABEL FROM DATA_TABLE);

This query is dynamically generated from Python and I am checking whether the date is already exist in the table before inserting and its not working in SQLite database. Getting this near "WHERE": syntax error error.

Am I doing something wrong ?

Thanks for your help.


Solution

  • I'm pretty sure that INSERT doesn't have a WHERE clause (the documentation doesn't mention any). What you can do:

    • create a unique index on LABEL
    • use INSERT OR FAIL
    • if that triggers an error, the row already exists.