java - Limit the number of rows in SQLite Database -


this question has answer here:

i have sqlite database save list of recent searches done in app. inserting this:

  /**    * inserts recentsearch recent searches table    *    * @param arecentsearch - 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());     } {       if (db.isopen()) {         db.close();       }     }   } 

and getting lastest 8 recent searches database this:

  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");     } {       cursor.close();     }     return recentsearcheslist;   } 

now worry database still saves recent search , doing pulling latests 8 recent searches line:

cursor cursor = db.query(dbhelper.recent_searches_table, dbhelper.recent_searches_columns_as_array, null, null, null, null, dbhelper.search_time + " desc limit 8");

so table have example 30 recent searches pulling latest 8.

i know how can either prevent more 8 entries being in table or delete latest 8 entries in table?

thank you

one possible way use trigger here while entering data particular table check , delete unnecessary rows table.

a trigger event-driven action run automatically when specified change operation ( insert, update , delete statement) performed on specified table. can go through triggers here in more details.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -