85

I am trying to port this line from MS SQL Server to SQLite

IF NOT EXISTS(SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received') 
    INSERT INTO EVENTTYPE (EventTypeName) VALUES ('ANI Received');

It seems that SQLite does not support IF NOT EXISTS or at least I can't make it work. Am I missing something simple? Is there a workaround?

4 Answers 4

130

How about this?

INSERT OR IGNORE INTO EVENTTYPE (EventTypeName) VALUES 'ANI Received'

(Untested as I don't have SQLite... however this link is quite descriptive.)

Additionally, this should also work:

INSERT INTO EVENTTYPE (EventTypeName)
SELECT 'ANI Received'
WHERE NOT EXISTS (SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received');
6
  • 15
    Thanks. However, it should be noted that INSERT OR IGNORE piece only works if EventTypeName is set to be unique. Feb 10, 2009 at 5:44
  • 2
    True. I assumed that it was unique given how it was used in the sample SQL. If not, the second method should be used.
    – beach
    Feb 10, 2009 at 5:52
  • Can the second method really be used if EventTypeName is not unique? I am trying to do something similar and I find that the SELECT WHERE NOT EXISTS clause is returning multiple rows, in fact every row where (the equivalent of )EventTypeName != 'ANI Received' is true.
    – Michael
    Jan 28, 2013 at 19:14
  • The intention is for EventTypeName to be unique. The new value 'ANI Received' is only inserted into table EVENTTYPE if it doesn't already exist in the table. This is what the WHERE NOT EXISTS comes in. The SELECT WHERE NOT EXISTS clause can only return a single row; there is not a FROM clause - there is no way multiple rows can be returned. Hope this clears it up.
    – beach
    Feb 4, 2013 at 6:59
  • 1
    This is very bad coding style. On every error the query will silently fail! From the docs: IGNORE When an applicable constraint violation occurs, the IGNORE resolution algorithm skips the one row that contains the constraint violation and continues processing subsequent rows of the SQL statement as if nothing went wrong. Other rows before and after the row that contained the constraint violation are inserted or updated normally. No error is returned when the IGNORE conflict resolution algorithm is used. May 1, 2015 at 23:58
19

If you want to ignore the insertion of existing value, there must be a Key field in your Table. Just create a table With Primary Key Field Like:

CREATE TABLE IF NOT EXISTS TblUsers (UserId INTEGER PRIMARY KEY, UserName varchar(100), ContactName varchar(100),Password varchar(100));

And Then Insert Or Replace / Insert Or Ignore Query on the Table Like:

INSERT OR REPLACE INTO TblUsers (UserId, UserName, ContactName ,Password) VALUES('1','UserName','ContactName','Password');

It Will Not Let it Re-Enter The Existing Primary key Value... This Is how you can Check Whether a Value exists in the table or not.

1

You can also set a Constraint on a Table with the KEY fields and set On Conflict "Ignore"

When an applicable constraint violation occurs, the IGNORE resolution algorithm skips the one row that contains the constraint violation and continues processing subsequent rows of the SQL statement as if nothing went wrong. Other rows before and after the row that contained the constraint violation are inserted or updated normally. No error is returned when the IGNORE conflict resolution algorithm is used.

SQLite Documentation

1

you can do "NOT EXISTS" with math, inverting EXISTS output:

SELECT (1-EXISTS(<Your_SELECT>));

Your_SELECT is: SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received'

If exists is 1 , 1-1 result is 0. If exists is 0 , 1-0 result is 1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.