Skip to content Skip to sidebar Skip to footer

Sqlite Table Constraint Unique And On Conflict Replace Usage

Please read before make it Duplicate. I am trying to insert longitude and latitude values in SQLite table and i want it to be unique value, after some research for that i have foun

Solution 1:

Change your table structure to this

privatestaticfinalString CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_LOCATIONS + "("
            + UID + " TEXT PRIMARY KEY," + ADDRESS + " TEXT,"
            + LONGITUDE + " TEXT," + LATITUDE + " TEXT,
            UNIQUE(" + LOGITUDE + "," + LATITUDE + ") ON CONFLICT REPLACE)";

Then when you do the insert use the below method

ContentValuesinsertValues=newContentValues();
insertValues.put(LATITUDE, latitude);
insertValues.put(LOGITUDE, longitude);
db.insertWithOnConflict(TABLE_LOCATIONS, null, insertValues, SQLiteDatabase.CONFLICT_REPLACE);

Post a Comment for "Sqlite Table Constraint Unique And On Conflict Replace Usage"