只允许Android SQLite独特的数据input?

在问题描述之前,我想指出我知道其他线程提出这个问题,但是没有一个能够解决我的问题。

我一直在使用BumpAPI共享应用程序,它在接收到大块后,将其保存到SQLite数据库中,以在列表视图活动中进行检索,这一切都工作正常,数据被保存,但是如果相同的文本是发送两次它会被一次又一次地保存和列表视图将显示这一点,从我读了我需要'独特'标识符? 但是对于SQL来说是全新的我对于实现这一点感到茫然,这里是我用来创build和添加条目的DataHelper类,是否有人会修改它或告诉我一个可能的解决scheme?

非常感谢

public class DataHelper { private static final String DATABASE_NAME = "tags.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "TagTable"; private Context context; private SQLiteDatabase db; private SQLiteStatement insertStmt; private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)"; public DataHelper(Context context) { this.context = context; OpenHelper openHelper = new OpenHelper(this.context); this.db = openHelper.getWritableDatabase(); this.insertStmt = this.db.compileStatement(INSERT); } public long insert(String name) { this.insertStmt.bindString(1, name); return this.insertStmt.executeInsert(); } public void deleteAll() { this.db.delete(TABLE_NAME, null, null); } public List<String> selectAll() { List<String> list = new ArrayList<String>(); Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, null, null, null, null, "name desc"); if (cursor.moveToFirst()) { do { list.add(cursor.getString(0)); } while (cursor.moveToNext()); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return list; } private static class OpenHelper extends SQLiteOpenHelper { OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)" + "text unique, " + "ON CONFLICT REPLACE"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } } 

Solutions Collecting From Web of "只允许Android SQLite独特的数据input?"

unique关键字添加到列中。

 db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT unique); 

看起来应该首先在Table Create语句上添加唯一索引

 db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)" + "**name** unique, " + "ON CONFLICT REPLACE"); 

它将防止有两个同名的条目

第二你可以做实际插入之前select检查数据的存在