我目前有一個SQLite數據庫來保存在我的應用程序中完成的最近搜索列表。我目前將是這樣的:限制SQLite數據庫中的行數
/**
* Inserts a RecentSearch into the Recent Searches Table
*
* @param aRecentSearch - The RecentSearch
*/
public void insertSearchIntoDB(RecentSearch aRecentSearch) {
SQLiteDatabase db = helper.getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.ENTITY_ID, aRecentSearch.getEntityId());
contentValues.put(DBHelper.SEARCH_PHRASE, aRecentSearch.getPhrase());
contentValues.put(DBHelper.SEARCH_TYPE, aRecentSearch.getSearchType().getItemId());
contentValues.put(DBHelper.SEARCH_CATEGORY_NAME, aRecentSearch.getCategoryName());
contentValues.put(DBHelper.SEARCH_TIME, aRecentSearch.getSearchTime());
contentValues.put(DBHelper.RATINGS, aRecentSearch.getSellerRatingsCount());
contentValues.put(DBHelper.STAR_COUNT, aRecentSearch.getSellerStarCount());
contentValues.put(DBHelper.SELLER_VERIFIED, TradeUtils.getBooleanAsInt(aRecentSearch.isSellerVerified()));
db.insertWithOnConflict(DBHelper.RECENT_SEARCHES_TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
} catch (Exception e) {
Log.e("TAG", "insertSearchIntoDB: " + aRecentSearch.getPhrase());
} finally {
if (db.isOpen()) {
db.close();
}
}
}
,並從數據庫中像這樣獲得最新的8個最近搜索:
public List<RecentSearch> getRecentSearchesFromDataBase() {
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query(DBHelper.RECENT_SEARCHES_TABLE, DBHelper.RECENT_SEARCHES_COLUMNS_AS_ARRAY, null, null, null, null, DBHelper.SEARCH_TIME + " DESC LIMIT 8");
List<RecentSearch> recentSearchesList = new ArrayList<>();
try {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
RecentSearch recentSearch = new RecentSearch();
int id_Index = cursor.getColumnIndex(DBHelper.ID);
int entityId_Index = cursor.getColumnIndex(DBHelper.ENTITY_ID);
int searchCategory_Index = cursor.getColumnIndex(DBHelper.SEARCH_CATEGORY_NAME);
int searchPhrase_Index = cursor.getColumnIndex(DBHelper.SEARCH_PHRASE);
int searchType_Index = cursor.getColumnIndex(DBHelper.SEARCH_TYPE);
int searchTime_Index = cursor.getColumnIndex(DBHelper.SEARCH_TIME);
int sellerRatings_Index = cursor.getColumnIndex(DBHelper.RATINGS);
int sellerVerified_Index = cursor.getColumnIndex(DBHelper.SELLER_VERIFIED);
int sellerStarCount_Index = cursor.getColumnIndex(DBHelper.STAR_COUNT);
recentSearch.setId(cursor.getInt(id_Index));
recentSearch.setEntityId(cursor.getInt(entityId_Index));
recentSearch.setPhrase(cursor.getString(searchPhrase_Index));
recentSearch.setCategoryName(cursor.getString(searchCategory_Index));
recentSearch.setSearchType(SearchType.getValueOrDefault(cursor.getInt(searchType_Index), SearchType.TRADE_SEARCH));
recentSearch.setSearchTime(cursor.getLong(searchTime_Index));
recentSearch.setSellerVerified(TradeUtils.getBooleanFromInt(cursor.getInt(sellerVerified_Index)));
recentSearch.setSellerRatingsCount(cursor.getInt(sellerRatings_Index));
recentSearch.setSellerStarCount(cursor.getInt(sellerStarCount_Index));
recentSearchesList.add(recentSearch);
}
}
} catch (Exception e) {
Log.e("TAG", "getRecentSearchesFromDataBase");
} finally {
cursor.close();
}
return recentSearchesList;
}
現在我擔心的是,該數據庫還保存所有的最近搜索和所有我我做的是剛剛拉latests最近8個搜索這一行:
光標光標= db.query(DBHelper.RECENT_SEARCHES_TABLE,DBHelper.RECENT_SEARCHES_COLUMNS_AS_ARRAY,NULL,NULL,NULL,NULL,DBHelper.SEARC H_TIME +「DESC LIMIT 8」);
因此,我的表將有例如30個最近的搜索,但我只是將最新的8
我想知道的是,我不是如何防止超過8項中所述桌子或刪除表中最新的8個條目除外?
謝謝
你需要僞代碼嗎? –