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 });
Related
I need to get contacts from an android phone. so I am using this code:
Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = ?", new String[] { displayName }, null);
while (phones.moveToNext()) {
//do work here
}
I want to tell the cursor to limit the response to 50 with something like "LIMIT 50". Where do I pass that information to the cursor? Please copy and paste my code and make edit there.
There is no limit in Android API, but you can apply it in your code like this:
Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = ?", new String[] { displayName }, null);
for (int i = 0; i < 50 && phones.moveToNext(); ++i) {
// do work here
}
phones.close();
Order by id DESC Limit 1:
db.query("table", null, "column=?", new String[]{"value"}, null, null, "id DESC", "50");
If you have the DB and the table name you can use that, but Content providers don't have a limit argument in their query statement.
mDb = new DBHelper (this);
Cursor c = mDB.getReadableDatase().query(
<Phone_Table_Name>,
new String[] { Phone.NUMBER, Phone.TYPE },
"DISPLAY_NAME = ?",
new String[] { displayName }, null),
null,
null,
null,
"50");
I am using query for search but i don't understand why one code is work and another one not work ?!
this is working:
String selectQuery = "SELECT * FROM " + TABLE_Soras
+" WHERE "+KEY_SoraName+" LIKE '%"+soraName+"%'";
Cursor cursor = db.rawQuery(selectQuery,null);
but this isn't get any result:
Cursor cursor = db.query(TABLE_Soras, new String[]{
KEY_SORA_NO, KEY_SoraName}, KEY_SoraName + "=?",
new String[]{soraName}, null, null, null, null);*/
Because in second case % is not appended into value and you didn't add LIKE clause into query. You need to use this:
KEY_SoraName + " like ?", new String[] {"%" +soraName + "%"} ...
Try this
Cursor cursor = db.query(TABLE_QuranSoras, new String[]{
KEY_SORA_NO, KEY_SoraName}, KEY_SoraName+" LIKE '%"+soraName+"%'", null, null, null, null);
Cursor cursor = your_database.query(MY_TABLE, new String[] {"first_column","second_column"},your_column + " LIKE '%"+your_criteria+"%'", null, null, null, null);
Try something like this and it will probably work!
I want to know how to use query method that have where clause with like property.
basically what i want is to select all the columns WHERE C_NAME column is LIKE keyWord.
I've tried this, but it doesn't work:
cursor = db.query(TABLE, null, C_NAME, new String[] {"like '"+keyWord+"'"}, null, null, null);
Look at the docs. They say:
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
so try this:
cursor = db.query(TABLE, C_NAME, new String[] {C_NAME + " like '" + keyWord + "'"}, null, null, null);
Also see this answer as well to see some examples on how to use the selectionArgs (4th argument above). That is where keyWord would have to go.
this query work perfect for me
Cursor cursor = db.query(true, TABLE_NAME, new String[]{ COLUMNS }, ROW + " LIKE ?", new > String[] { "%" + name + "%" }, null, null, null, null);
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);