android - Cursor does not get values from database -


im trying values database using cursor. problem when try values out of db cursor out of bounds. im sure values want exists in db because have other method can values. want value column_fields_parameter

log cat:

05-09 11:24:17.872: e/androidruntime(4113): fatal exception: main 05-09 11:24:17.872: e/androidruntime(4113): android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 05-09 11:24:17.872: e/androidruntime(4113):     @ android.database.abstractcursor.checkposition(abstractcursor.java:424) 

here code: putting values:

public void putparameters(int[] parameters, string exercise) {      string parameterslist = "";      for(int = 0; < parameters.length; i++){          if (parameters[i] != 0)         {             parameterslist = parameterslist + " " + parameters[i];         }     }         contentvalues cv = new contentvalues();     cv.put(column_fields_parameter, parameterslist);     cv.put(column_exercise, exercise);      string whereclause = column_fields_parameter + "= ? , " + column_exercise + "= '" +  exercise + "'";     string[] columns = new string[]{column_exercise, column_fields_parameter};      cursor c = ourdatabase.query(table_name, columns, whereclause , null, null, null, null, null);      if(c == null)         ourdatabase.insert(table_name, null, cv);     else         ourdatabase.update(table_name, cv, whereclause, null); }      public string getparameters(string exercise){      string foo = "";      string whereclause = column_fields_parameter + "= ? , " + column_exercise + "= '" +  exercise + "'";     string[] columns = new string[]{column_exercise, column_fields_parameter};      cursor c = ourdatabase.query(table_name, columns, whereclause , null, null, null, null);      int parameter = c.getcolumnindex(column_fields_parameter);          if (c != null)         {             c.movetofirst();             foo = c.getstring(parameter);             return foo;         }         return foo; } 

you're missing whereparameters goes whereclause. try this:

string whereclause = column_fields_parameter + "= ? , " + column_exercise + "= '?'"; string[] whereparams = {field, exercise};  cursor c = ourdatabase.query(table_name, columns, whereclause , whereparams, null, null, null); 

note whereclause has 2 ?. each 1 need provide value, in string[] whereparams , pass query. substitue each ? correct value (in example above field , exercise. protects sql injection (so user cannot enter sql commands in exercise.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -