2016-11-10 102 views
0

我目前有一個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個條目除外?

謝謝

+0

你需要僞代碼嗎? –

回答

0

一種可能的方式是在輸入數據到特定表檢查,並從該表中刪除任何不必要的行使用觸發這裏。

A 觸發器是一個事件驅動的操作,當在指定的表上執行指定的更改操作(INSERT,UPDATE和DELETE語句)時會自動運行。您可以更詳細地瞭解觸發器here

+0

我發現這(http://stackoverflow.com/a/18677756/4578531)在哪裏使用觸發器,我有點困惑,但我在哪裏使用這個觸發器? – x10sion

+0

你不需要從你的代碼中自己調用觸發器。只需創建與我們在數據庫中創建表相同的觸發器。數據庫將負責在您創建觸發器的每個事件上調用該事件。 –

+0

如果你不想寫觸發器。您需要在插入查詢後每次執行刪除查詢以刪除不必要的行。 –