I am trying to make a query to sqlite android to see for example how many users of a given username exist in a table.
This is my function. I must specify that “getContentResolver() != null” and so is variable name.
private int findSelectedUser(String name) {
int count = 0;
try {
String[] whereArgs = new String[] {name};
String[] PROJECTION = new String[] { MyProvider.SETTINGS_USERNAME };
Cursor c = getContentResolver().query(MyProvider.SETTINGS_URI,
PROJECTION, MyProvider.SETTINGS_USERNAME , whereArgs, null);
if (c != null) {
count = c.getCount();
c.close();
}
} catch (NullPointerException e) {
}
System.out.println("Found something? " + count);
return count;
}
And after running i receive the error from the subject…and don’t get it. In my where clause i have one column, in my where arguments one value.
Please help me make some sence of this, Thank you.
I guess that works:
If you want to use
whereArgsyou have to have the same amount of?in thewhereas you have itemswhereArgswhereArgswill replace the?in the final database querythat results in
name = 'Peter' OR name = 'Jim'for the query.Btw: don’t
catch(NullPointerException e)– make your code safe so they can’t happen