Sarp Kaya
Sarp Kaya

Reputation: 3784

SQLite avoiding duplicate entries

I haven't used SQLite before. As far as I remember, when I was using MySQL, if I create a constraint with the option of UNIQUE I would not receive any errors if I try to add a duplicate element.

It seems like that's not the case for SQLite.

This is how I create my Table:

    // Create table for Wi-Fi Information
    myDB.execSQL("CREATE TABLE IF NOT EXISTS " + WIFI_DATABASE_TABLE
            + " (WifiName VARCHAR UNIQUE);");

This is how I insert elements

    if (tab == DBTable.WIFI_DATABASE) {
        myDB.execSQL("INSERT INTO " + WIFI_DATABASE_TABLE
                + " (WifiName)" + SQLValues + ";");
    }

My problem is when I insert the same element I am getting Column WifiName name not unique (code 19) error. What I want is simply not to see any errors. How can I do that without playing around with catching exceptions?

Upvotes: 0

Views: 1113

Answers (2)

swandog
swandog

Reputation: 749

The syntax for conflict resolution on an INSERT is to use OR IGNORE

INSERT OR IGNORE INTO ...

More info: http://www.sqlite.org/lang_conflict.html

Upvotes: 1

laalto
laalto

Reputation: 152817

Specify conflict resolution so that the operation doesn't fail and no exception is thrown:

INSERT OR IGNORE INTO ...;

Reference

Upvotes: 2

Related Questions