在我通過問題進行描述之前,我想指出我知道其他線程提出這個問題,但是沒有人能夠解決我的問題。只允許Android SQLite的唯一數據輸入?
我一直在使用BumpAPI來處理共享應用程序,它在接收到塊後將其保存到SQLite數據庫以便在列表視圖活動中進行檢索,但這一切都工作正常且數據已保存,但如果相同的文本被髮送兩次,它會被一次又一次地保存,列表視圖會顯示出來,從我讀過的內容中我需要'UNIQUE'標識符?但是對於SQL來說是全新的我對實現這一點感到茫然,這裏是我用來創建和添加條目的DataHelper類,是否有人會修改它或告訴我一個可能的解決方案?
非常感謝
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);
}
}
}
似乎也沒有工作,如果我查看SQL數據庫有重複條目的再次HM – Broak
整理出來自己到底!恥辱noone甚至試圖幫助解答,但嘿,繼承人解決方案,如果有人需要它: db.execSQL(「CREATE TABLE」+ TABLE_NAME +「(id INTEGER PRIMARY KEY,name TEXT unique); – Broak