My query is regarding the creation of the function parameters for getting data from the database.
I want the cursor to returns only specific rows instead of complete recordset.
c= getContentResolver().query(uri, null, "column1=? \"mysearch\" or column2=? "\mysearch"\", null, "date DESC");
I want to get the results where column1 like "mysearch" or column2 like "mysearch".
In the above example i have tried with some hardcoded values.
Regards,
Nirav
Got it after some workaround.
The correct query will be
String searchQuery = "column1 like '%" + searchKey
+ "%' or column2 like '%" + searchKey + "%'";
c = getContentResolver().query(uri, null, searchQuery, null,
"date DESC");
Thanks,
Nirav
Related
I am using a content provider and need to query a table where one column of the row = thing1 and another column of that same row = thing2.
i know you can query a table for just one condition like:
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "'", null, null);
now how can i query for two conditions? any help would be appreciated.
This should work:
Cursor c = contentResolver.query(content_uri, projection, "col1=? AND col2=?", args, null);
where args is a String[] of values to substitute in for the ? in the query.
Try this
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "' AND "+variable2 + "='"+thing2+"'" , null, null);
My question is why this query does not work?
Cursor c = db.rawQuery("SELECT * FROM tbl_staff WHERE PMajor = '%" + spin.getSelectedItem().toString() + "%'", null);
Cursor c: it is a cursor for handling my query
tbl_staff: my table that consist of PName, PMajor, PCert
spin: is spinner that has values which I need for my database query.
When I use:
if (c.moveToNext())
else (log.d("error query","couldn't do the query!");)
It goes to else statement and moveToNext() doesn't work.
Instead of using =, which checks for equality, use keyword LIKE, which matches a pattern.
Cursor c = db.rawQuery("SELECT * FROM tbl_staff WHERE PMajor LIKE '%" + spin.getSelectedItem().toString() + "%'", null);
I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel
Cursor mCursor = mDb.query(true, CUSTOMER_TABLE_NAME,
new String[] {GENERIC_ID_KEY, ADDRESS_KEY, PHONE_KEY, EMAIL_KEY,CUSTOMER_NAME_KEY},
GENERIC_ID_KEY + "=" + customerDbId, null,
null, null, null, null);
How should I modify this command if GENERIC_ID_KEY and customerDbId is string.
I tried using like in b/w but still negative results, n error is thrown.
if customerDbId is a string type then place it in between quote
as
GENERIC_ID_KEY + "= '" + customerDbId + "'"
It's a cursor problem. Be sure curser is the first state before selection.
ALL,
I am trying to execute following piece of code:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, "ContactsContract.Contacts.DISPLAY_NAME LIKE '" + name + "'", null, null );
However when running this code, I am getting SQLite exception: "Don't know such file ContactsContract.Contacts.DISPLAY_NAME:, while compiling "....."".
The problem is that I don't know in advance what will "name" contain, hence using "LIKE" clause.
Is there a better way to perform such operation? Or I am just doing it incorrectly?
Thank you in advance for any help.
Or I am just doing it incorrectly?
ContactsContract.Contacts.DISPLAY_NAME is a Java construct. Use:
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE '" + name + "'", null, null );
Or, use positional parameters:
Cursor cur = cr.query( ContactContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", args, null );
where args is a one-element string array containing your name.