Search code examples
javaandroidsqlitesqliteopenhelper

Insert only if timestamp is unoccupied


I am using Android / Java and SQLite3 / SQLiteOpenHelper, with the Cursors and ContentValues.

I have a timestamp field in my table. I want to ensure that if someone is adding a new row, the row is not added if the timestamp is already occupied (so I do not want more than one value per timestamp).

Can this be enforced efficiently? Or do I have to perform a select statement every single time I perform an insertion to check first?


Solution

  • Add a UNIQUE qualifier on the timestamp field. You can then choose the behavior you want by calling inserWithOnConflict() and passing CONFLICT_FAIL as the conflict algorithm argument.