java - Android SQLite: Running the Same Query with Different Parameters Multiple Times Efficiently -


i have table in sqlite database:

species:

_id  |  species  |  description  ---------------------------   1  |  aardvark |  description aardvark   2  |  aardwolf |  description aardwolf   3  |  caracal  |  description caracal 

i receive list of ids server , want display species corresponding ids received.

now aware of few options:

1. obvious , naive way of doing along lines of:

sqlitedatabase db = this.opendatabase(); (int id : idlist) {     cursor cursorspecies = db.query(true, table_species, new string[] {col_species_species},             col_id + "=?", id, null, null, null, null);     cursorspecies.movetonext();     specieslist.add(cursorspecies.getstring(0));     cursorspecies.close(); } 

this execute way many operations , assume multiple small "disk" reads, slow.

2. option use sqlitestatement returns single value, doesn't work example , shouldn't used queries anyway.

3. yet option manually concatenate conditions raw sql query, along lines of:

sqlitedatabase db = this.opendatabase(); string query = "select * " + table_species + " "; (int id : idlist) {     query += col_id + "=" + id + " or "; } // aware end in " or" not point of example please ignore it. cursor cursorspecies  = db.rawquery(query, null); // use cursor , close it.  while should work decently well, large query break query string length limit not ideal either. 

all of these examples work extent have pitfalls. somehow feel missing solution this, hence question:

what proper way of executing type of query?

thank you.

for special case in question, consider where id in (x,y,z, ...).

to address question in title , not special case in question body:

the android sqlite api not versatile in regard.

in raw sqlite3 c api 1 single sqlite3_prepare*() call obtain sqlite3_statement , bind arguments in place, sqlite3_step() row(s) , reset statement reuse new argument bindings.

in android api, statement corresponds cursor , stepping equivalent of moving cursor. reset , rebind functionality available in sqlitecursor requery() , setselectionarguments().

so try along following lines:

  1. do regular query selection args.

  2. assuming default cursor factory, cast resulting cursor sqlitecursor.

  3. access row(s) need.

  4. update selection args setselectionargs()

  5. requery()

  6. goto 3 unless done


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -