Two SQLite tables in my Android App:
- PoweredEquip which contains all powered equipment units and their associated trailer
- Trailers which contain all trailers not currently tied to a powered unit
When populating these two tables, I need to only insert into the Trailers table if the VIN doesn’t exist in the PoweredEquip table. So in T-SQL, I would do something like
If Not Exists (Select VIN From PoweredEquip Where VIN = _VIN)
Insert Into Trailers Values (.....)
Without having to do a Select on PoweredEquip, opening up a DataReader and checking if HasRows is False in order to do the Insert, I was hoping there was an If Not Exists (Select…) equivalent in SQLite.
There is no direct equivalent for that in SQLite. Inserts don’t support any conditions (unless you do it via INSERT INTO TABLE SELECT..’. It might be possible to build something similar with an
INSTEAD OF INSERTtrigger that does the insert or not based on aCASE. Or with anAFTER INSERTtrigger that deletes rows that don’t belong into the table.untested:
Edit the way mentioned in the other answer should work too: