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);
Related
I have the following query:
Cursor c = sd.query(itemsTable.Table_Name, columns, itemsTable.ItemNumber + "= ?", selectionArgs, null, null, null);
I need to be able to do this:
'SQL Query I am running on the database using for testing
select * from items where rtrim(itemnumber) = '292664'
Where itemnumber is my selectionArgs.
The SQL query returns the correct results but the cursor query returns null.
I would rather not change the way I am running queries and would like to know if there is a way to convert my existing query to work the same as the SQL query.
Try the following:
String selection = "RTRIM(" + itemsTable.ItemNumber + ") = ?";
String[] selectionArgs = new String[] {"292664"}; // or whatever
Cursor c = sd.query(itemsTable.Table_Name, columns, selection, selectionArgs, null, null, null);
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'm new to SQLite. I'm using this query in order to extract all rows from a column for a specific user:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null, null, null, null, null);
This is an example of table:
|group-----ID|
|------------|
|one-------me|
|one------you|
|two-------me|
|one-------me|
So the query for user "me" extracts "one, two, one". How can I filter the result in order to obtain a value just one time? Like "one, two"
Thanks
Use the Group By parameter with the appropriate column name:
Cursor c = db.query(true, TABLE, COLUMN, USER + "= '" + user + "'", null,
"group", null, null, null);
Since there are nine parameters in this method and three nearly identical methods...
public Cursor query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy, /* this one */
String having,
String orderBy,
String limit)
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
i have a table with columns ID,SUBJECT,BRANCH
i have select the rows which satisfies the follwing conditions
id=rollno
branch=cse
subject=any subject
db.query(DATABASE_TABLE, new String[] {
ID,SUBJECT,BRANCH},
where clause,
null,
null,
null,
null);
how to write where clause here to satisfy my conditions?
You can use db.rawQuery methos also
Cursor cursor= db.rawQuery("select * from <table_name> where id='1' and
branch='" + branchName + "' and subject='" + subjectName+ "'");
add Where clauses with " AND ".. Pretty simple.. Each condition is seperated by the same..
here is an example
String addrWhere = ContactsContract.Data.CONTACT_ID+" = ? AND "+ContactsContract.Data.MIMETYPE+" = ?"; //this is where clause and below is corresponding selection args..
String[] addrWhereParams = new String[]{data.getContac_ID(),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};