限制SQLite数据库

时间:2016-11-10 06:24:48

标签: java android database 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;
  }

现在我的担心是数据库仍然保存了所有最近的搜索,我正在做的只是用这一行拉最近8次搜索:

  

Cursor cursor = db.query(DBHelper.RECENT_SEARCHES_TABLE,DBHelper.RECENT_SEARCHES_COLUMNS_AS_ARRAY,null,null,null,null,DBHelper.SEARCH_TIME +“DESC LIMIT 8”);

所以我的桌子最近有30个搜索,但我只提取最新的8个。

我想知道如何阻止表中超过8个条目或删除表中最新的8个条目?

谢谢

1 个答案:

答案 0 :(得分:0)

一种可能的方法是在此处使用触发器,同时将数据输入特定表以检查和删除该表中的任何不必要的行。

触发器是一种事件驱动的操作,在指定的表上执行指定的更改操作(INSERT,UPDATE和DELETE语句)时自动运行。您可以更详细地浏览触发器here