I’m using sqlite in android app and I want to make insert in table by 2 unique row values. For example:
id| column1 | column2 | column3|
1. 1 2 3
2. 2 3 4
Unique columns – COLUMN1 and COLUMN2
INSERT OR REPLACE INTO HISTORY (id, column1, column2, column3) VALUES (null, 2, 3, 6);
But this query shouldn’t works ’cause 1st and 2nd values isn’t unique in table
Thnx.
If I understand your question, simply use the
UNIQUEconstraint when building your table:Then if you try these
INSERTstatements:The second will fail because
column1andcolumn2already exist.Understand that if you use
INSERT OR REPLACEin my example, the second statement will replace the first (no exception thrown).Typically you would use
ALTER TABLEto add a constraint to your existing tables, but SQLite does not allow this… However you can workaround this limitation: