I've an cursor that fills up my ListView in Android.
My code of cursor:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto <> '70027, 70029, 70024'", null, null, null, null);
It is not working. It continues filling the list with these values: 70027, 70029, 70024.
What am I doing wrong?
Thanks.
Instead of using <>, you need to use NOT IN which offers look-up with in a set of values. So try changing your query to something like:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto NOT IN (70027, 70029, 70024)", null, null, null, null);
Try this code:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto <> ? AND codigoproducto <> ? AND codigoproducto <> ?',
new String[]{"70027", "70029", "70024"},
null, null, null);
Related
I have a database in my app, which I fill with data and an ID.
Then I request the database with the ID's I want:
Cursor cursor = contentResolver.query(uriPath, null, "ID = ?", new String[]{id1, id2, etc.}, null);
This retrieves me a cursor with all requested ID's .
How can I retrieve all objects in my UriPath ?
Just enter the Uri and leave the rest null
Cursor cursor = contentResolver.query(uriPath, null, null, null, null);
I write
Cursor c = db.query("contacts", null, "label <> ?", new String[]{cons.LABEL_SKIP}, null, null, "name");
cons.LABEL_SKIP equals Skipped;
I want to select data base lines whose label not equals Skipped, but answer comes empty.
Try it this way
Cursor c = db.query("contacts", null, "label != ?",
new String[] { cons.LABEL_SKIP });
My intention is to display the contacts in sorting order using content resolver in android.
For that I'm writing:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
It needs that the last parameter in query method should not be null for sorting the elements by Name. Which part of code I have to replace the null parameter to achieve sorting by name?
To sort result according to name use Phone.DISPLAY_NAME constant with ASC as last parameter to query method. do it as:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
new String[] { id },
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
It would be better to use SORT_KEY_PRIMARY or SORT_KEY_ALTERNATIVE on API level 11 and later.
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null,
ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
You can use Upper() to sort for both lower as well as upper case contact name.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
The ContentResolver.query() method takes many arguments but to sort the content provider records, you have to edit the last argument of this method.
It should be like this:
Cursor cursor=getContentProvider().query(.......,"DISPLAY_NAME ASC")
This will arrange the contacts in Ascending order of their name.
Note: This argument should be in a String datatype.
You can use the Desc string using these lines to sort out to see the recent contact
android.database.Cursor cursor = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP +" DESC");
I'm trying to search data on database based on 2 conditions (entryId=rowId & category=cat_id) like below
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"}, "entryId="+ rowId + "& category="+cat_id, null, null, null, null, null);
Its not working. But when I try to do it using only one condition like below then it works well.
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"}, "entryId="+ rowId, null, null, null, null, null);
Can you help me telling how can I run this query with multiple conditions?
In SQLite & is different from AND, simply use:
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"},
"entryId="+ rowId + " AND category="+cat_id, null, null, null, null, null);
// Change & to AND... here: ^^^
How do I use the String[] selectionArgs in SQLiteDatabase.query()? I wish I could just set it to null, as I have no use for it. I am just trying to load an entire unsorted table from a database into a Cursor. Any suggestions on how to achieve this?
selectionArgs replace any question marks in the selection string.
for example:
String[] args = { "first string", "second#string.com" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);
as for your question - you can use null
Yes, you may set all parameters to null except the table name.
for example:
Cursor cursor = db.query("TABLE_NAME", null, null, null, null, null, null);