我想从sqlite中删除一行但是我的senario是当我第一次输入文本字段时它应该插入到表中,当我再次输入具有相同值的文本字段时,它不应该作为第二行输入,它应该删除第一个条目并获得更新,表ID应该只保留为1。
我不知道怎么做,请在我第一次做数据库时建议我。
感谢。
创建表:
"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" +
")";
答案 0 :(得分:0)
您需要将UNIQUE
约束添加到您希望不希望具有重复值的表的列中。如下所示
COLUMN_NAME+ " TEXT UNIQUE, "
答案 1 :(得分:0)
它没有你想象的那么强硬。在创建数据库时,请将值设为uniique:
db.execSQL("CREATE TABLE secure (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT UNIQUE ON CONFLICT REPLACE," +
"value TEXT" +
");");
当您尝试在上表中反复插入相同的“名称”时,sqlite将自动为您插入和替换。因此没有重复的值。