android - Updating a single column is creating sqlite syntax error -
i'm not sure i'm doing wrong, i'm trying update single integer value in column of table 1 0. when creating database, set values of column 0 using:
for (int = 0; < setups.length; i++) { contentvalues values = new contentvalues(); values.put(jokedbcontract.tbljoke.column_name_setup, setups[i]); values.put(jokedbcontract.tbljoke.column_name_punchline, punchlines[i]); values.put(jokedbcontract.tbljoke.column_name_used, 0); db.insert(jokedbcontract.tbljoke.table_name, null, values); }
then, in actual activity, i'm doing:
private void findnewjoke() { jokedb jokedb = jokedb.getinstance(this); sqlitedatabase thedb = jokedb.getdb(); string selection = jokedbcontract.tbljoke.column_name_used + "=" + 0; // query database joke has not been used, update fields // thejoke , thepunchline appropriately string[] columns = {jokedbcontract.tbljoke._id, jokedbcontract.tbljoke.column_name_punchline, jokedbcontract.tbljoke.column_name_setup, jokedbcontract.tbljoke.column_name_used}; cursor c = thedb.query(jokedbcontract.tbljoke.table_name, columns, selection, null, null, null, null); if (c.movetofirst() == false) { toast.maketext(this, r.string.error_retrieving_joke, toast.length_long).show(); log.e(getstring(r.string.app_name),"no jokes retreived db in jokeactivity.findnewjoke()!"); } else { contentvalues values = new contentvalues(); thesetup = c.getstring(c.getcolumnindexorthrow(jokedbcontract.tbljoke.column_name_setup)); thepunchline = c.getstring(c.getcolumnindexorthrow(jokedbcontract.tbljoke.column_name_punchline)); string updateselection = jokedbcontract.tbljoke.column_name_setup + "=" + thesetup; values.put(jokedbcontract.tbljoke.column_name_used, 1); thedb.update(jokedbcontract.tbljoke.table_name, values, updateselection, null); } }
i'm getting error on update:
java.lang.runtimeexception: .... while compiling: update jokes set used=? setup=why programmers mix halloween , christmas?
it seems though i'm not getting actual value set used column. program cycle through jokes used=0, sets used 1 when has been viewed. query pulls jokes aren't used yet. have feeling i'm missing simple, 1 can hope.
i think having problems quotation marks.
example:
string updateselection = jokedbcontract.tbljoke.column_name_setup + "=\"" + thesetup + "\"";
however, recommended way this, be:
thedb.update(jokedbcontract.tbljoke.table_name, values, jokedbcontract.tbljoke.column_name_setup + " = ?", new string[] { thesetup });
it better use field = ?, because helps sqlite cache queries (i believe).
Comments
Post a Comment