最初に ID ジェネレーターを作成します。
private int generateId() {
// if the table is empty returns -1
int result = -1;
String sql = "SELECT id FROM table order by id asc";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
//if any id is decremented
if (cursor.moveToFirst())
result = (cursor.getInt(0) -1);
cursor.close();
db.close();
return result;
}
これが初期値になり、別の値に置き換えることができます
// if the table is empty returns -1
int result = -1;
次に、挿入で使用します。
public void add(Type type) {
int id = this.generateId();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("id", id);
cv.put("value",type.value)
...
db.insert(table, null, cv);
cv.clear();
db.close();
}
誰にも別の解決策がありますか?