2

I have the following code in an Update method of a ContentProvider subclass:

When I use placeholders for the Where clause, my update does not work (count equals 0):

Does not work:

rowID = uri.getPathSegments().get(1);
whereStr = "?=?";
count = db.update(PathSQLTable.TABLE_NAME, values, whereStr, new String[]{PathSQLTable.ID_KEY,rowID});

But if I do the substituion manually, everything works fine.

This works:

rowID = uri.getPathSegments().get(1);
whereStr = PathSQLTable.ID_KEY + "=" + rowID;
count = db.update(PathSQLTable.TABLE_NAME, values, whereStr, null);

My Question is-- is there a bug in the placeholder logic (as suggested in this post) or am I doing something wrong?

Community
  • 1
  • 1
CjS
  • 2,037
  • 2
  • 21
  • 29

1 Answers1

3

it works only with the input parameters in the sql query, not with the columns name. So it should work if you try this:

rowID = uri.getPathSegments().get(1);
whereStr = PathSQLTable.ID_KEY + " = ?";
count = db.update(PathSQLTable.TABLE_NAME, values, whereStr,new String[]{rowID});
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Interesting. Do you happen to know what gets sent as the SQL query in my "doesn't work" case? No errors are thrown by android, it just returns zero. – CjS Mar 18 '12 at 06:29
  • i only know that column names are provided without using `?`. Its the same as in C# for e.g. `select * from Customers where city = @City` where **@City** is provided via `cmd.Parameters.AddWithValue("@City", "Stockholm");`. However, in your "doesn't work" query, i believe it translates `?` as for example **'ID_KEY'=6**, so ID_KEY is taken as a string value, not a column name, but its just my assumption :) – waqaslam Mar 18 '12 at 11:59
  • Ok, and that would explain why it's a valid query that returns zero records. Thanks. – CjS Mar 18 '12 at 13:09