在那里,我使用此函数在SQLite android中插入/删除记录。
//添加新bookMark
public void addBookMark(BookMark bookMark) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, bookMark.getTitle()); // BookMark Name
values.put(KEY_URL, bookMark.getURL()); // BookMark Phone
// Inserting Row
db.insert(TABLE_BOOKMARKS, null, values);
db.close(); // Closing database connection
}
//删除单个bookMark
public void deleteBookMark(BookMark bookMark) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_BOOKMARKS, KEY_ID + " = ?",
new String[] { String.valueOf(bookMark.getID()) });
db.close();
}
现在我想在DB中只保留最后100条记录。 (我想在插入新记录时检查。如果计数超过100 ...代码删除最旧的记录。
像这样的aql代码:DELETE FROM TABLE_BOOKMARKS where KEY_ID NOT IN (SELECT KEY_ID from TABLE_BOOKMARKS ORDER BY KEY_ID DESC LIMIT 100)
我认为顶级代码是正确的,但我如何在此代码中使用它:
//添加新bookMark并始终保持100条记录。
public void addBookMarkAndKeep100(BookMark bookMark) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, bookMark.getTitle()); // BookMark TITLE
values.put(KEY_URL, bookMark.getURL()); // BookMark URL
// Inserting Row
db.insert(TABLE_BOOKMARKS, null, values);
db.delete(?);
db.close(); // Closing database connection
}