Using PHP PDO with SQLite, I can write a PDO call to check if a certain row exists (by checking for the primary key) in the table, and then if it doesn't, write another PDO call to create the row. But I feel like it should be somehow possible to do in one command… similar to the "CREATE TABLE IF NOT EXISTS". Is there anything like "INSERT ROW IF NOT EXISTS"?
Asked
Active
Viewed 287 times
-1
-
1don;t you think you should check INSERT COMMAND manual – Satya Dec 08 '13 at 06:42
-
@MisterMelancholy, I get a PDO error when I try following that answer. – Dan Goodspeed Dec 08 '13 at 07:16
-
@DanGoodspeed Then *include* the relevant error (and *show* what was tried which resulted in said error). – user2864740 Dec 08 '13 at 08:04
-
Different approaches discussed here http://stackoverflow.com/questions/2779823/sqlite-query-to-insert-a-record-if-not-exists and http://stackoverflow.com/questions/7481161/prevent-insertion-if-the-records-already-exist-in-sqlite?rq=1 – user2864740 Dec 08 '13 at 08:06
1 Answers
1
Use
INSERT OR IGNORE INTO ...
where your table has some constraint (such as a PRIMARY KEY
column) that will cause a conflict when a row is attempted to be inserted again.
Reference: http://www.sqlite.org/lang_conflict.html

laalto
- 150,114
- 66
- 286
- 303