0

I want to delete an row from sqlite but my senario is when i first enter a text field it should be inserted to the table and when I again enter the text field with same value it should not be entered as second row,It should be deleting the first entry and get updated and table Id should remain 1 only.

I dont know how to do it,Please suggest me as I doing first time the database.

Thanks.

Table creation:

            "CREATE TABLE secure" + DatabaseContract.RetailDatabaseEntry.TABLE_NAME_SETTINGS + " (" +
                    DatabaseContract.RetailDatabaseEntry.COLUMN_ID_SETTINGS+ " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    DatabaseContract.RetailDatabaseEntry.COLUMN_FIRSTNAME + " TEXT UNIQUE ON CONFLICT REPLACE,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_LASTNAME +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_PHONENUMBER +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_EMAILADDRESS +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_ADDRESSLINE1 +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_ADDRESSLINE2 +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_LOCALITY +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_CITY +" TEXT,"+
                    DatabaseContract.RetailDatabaseEntry.COLUMN_PINCODE +" TEXT" + 
                    ")";
amalBit
  • 12,041
  • 6
  • 77
  • 94

2 Answers2

0

You need to add UNIQUE constraint to the column of the table you want dont want to have duplicated values. like below

COLUMN_NAME+ " TEXT UNIQUE, "
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Its not as tough as you think. While you create your database, make the value uniique like:

db.execSQL("CREATE TABLE secure (" +
            "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "name TEXT UNIQUE ON CONFLICT REPLACE," +
            "value TEXT" +
            ");");

NOw when you try to insert same "name" again and again in the above table, sqlite will automatically insert and replace for you. Thus no duplicate values.

amalBit
  • 12,041
  • 6
  • 77
  • 94