String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER " + " = '1''";
String[] selectionArgs = String.valueOf(1);
Why do we put " = '1' " in the Selecion Query ?
and Also the Reason for String.valueOf(1);
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER " + " = '1''";
String[] selectionArgs = String.valueOf(1);
Why do we put " = '1' " in the Selecion Query ?
and Also the Reason for String.valueOf(1);
You probably meant this:
or this
or this
or just this
All four selections have the same results: Contacts that have a phone number.
SQLite (the database type that's used on Android) doesn't support Booleans (see Datatypes in SQLite Version 3),
trueandfalseare usually stored as integers1and0. So, to check if a boolean field is true you check if it has the value1.Code snippets #1 and #2 use a prepared statement which contains a parameter or placeholder that's replaced with the first value of the
selectionArgsarray (since it's the first parameter in the query). This is a technique to protect against SQL injection and it often makes your queries better readable.selectionArgshas to be an array ofStringvalues, so the integer value1in snippet #1 needs to be converted to aStringfirst (usingString.valueOf(int)actually only makes sense if you pass a variable rather than a constant number).If you don't use any variables in your select statement you can just insert all values into the statement not using placeholders and
selectionArgsas in code snippets #3 and #4. Since SQLite is dynamically typed it doesn't matter if you check for equality to1or'1', the SQL interpreter will get it right either way.