Skip to content Skip to sidebar Skip to footer

Sqlite: Prevent Duplicates

I would like to create a table to store device settings. The table has three rows: id, parameter_name and parameter_value. The table was created by executing the following query st

Solution 1:

You can use the column constraint UNIQUE.

The UNIQUE constraint causes an unique index to be created on the specified columns.

Solution 2:

I would create a UNIQUE CONSTRAINT in the database table definition. That will automatically prevent duplicate entries and it's pretty much the only concurrency-safe way to do it!

Also, don't forget about CHECK CONSTRAINT which may be more helpful if you need to define more complex rules for the data in you tables.

Solution 3:

Are you trying to prevent it through code, or in the database?

If trying to prevent from code you'll need to do a SELECT and see if the row already exists.

If trying to prevent it from the database you'll need to add a UNIQUE constraint on whichever row you don't want to allow duplicates on.

Solution 4:

In your case, the correct primary key would have been parameter_name.

Post a Comment for "Sqlite: Prevent Duplicates"