2

I am trying to retrieve sms by phone number but I don't get any result. (I do get results if I replace where and where_args by null)

String where = "address=?";
String[] where_args = new String[]{"+33 1 23 45 67 89"};
contentResolver.query(uri, new String[]{"*"}, where, where_args, null);

I suspect that there is a problem with the ? because of the spaces, so I tried where = "address='?'"; andwhere = "address=\"?\"";` but none worked

Any idea ? Thanks !

Thomas
  • 8,306
  • 8
  • 53
  • 92
  • Try wrapping your selection arguments in a single quote: String[] where_args = new String[] {"\'+33 1 23 45 67 89\'"}; – Larry Schiefer Jan 10 '14 at 04:27
  • I have changed the version of Android Studio I was working on. Before I would get an exception SQLiteException: near "?" syntax error. I no longer have the exception when running code from my question, but cursor is empty. – Thomas Jan 12 '14 at 09:43

1 Answers1

1

Have you tried the following to ensure part of it works correctly?

String where = "address = '+33 1 23 45 67 89'";
contentResolver.query(uri, 
                      new String[]{"*"}, 
                      where, null, null);

Try creating the array in the query call.

String where = "address = ?";
String where_args = "+33 1 23 45 67 89";
contentResolver.query(uri, 
                      new String[]{"*"}, 
                      where, 
                      new String[]{where_args}, 
                      null);

Also, check that your projection argument isn't causing issues. Set that parameter specifically to the column you are searching, at least for testing purposes, to see if that is altering the selection argument in anyway.

More examples below:

Community
  • 1
  • 1
JSuar
  • 21,056
  • 4
  • 39
  • 83
  • Yes, your first suggestion works on my computer, it is a non empty result. My second option was to do `address='xxx' OR address='yyy' OR ...` and not use whereArgs, which is working, but I found it a little bit more messy (sql injection prone, ...) than what I would have liked – Thomas Jan 12 '14 at 09:46
  • @Thomas Does setting the project to the specific columns help at all? Also, what about creating the array in the query call like my second example? – JSuar Jan 12 '14 at 18:41