At the moment i'm currently using the query builder to pull all my records from my database and they are currently organised by day however I its sorts in althabetical order,
public Cursor fetchAll() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_MODULECODE,
KEY_DAY, KEY_TIME, KEY_DURATION, KEY_TYPE, KEY_ROOM},null, null, null, null,"Day");
}
I know after some googling, that I need to replace the final section with a custom search order.
something along the lines of :
select _id, busnum,
case cast (strftime('%w', servdate) as integer)
when 0 then 'Sunday'
when 1 then 'Monday'
when 2 then 'Tuesday'
when 3 then 'Wednesday'
when 4 then 'Thursday'
when 5 then 'Friday'
else 'Saturday' end as servdayofweek
from tb1
where ...
From:
http://stackoverflow.com/questions/4319302/sqlite-return-as-day-of-week
so far i have been unable to figure out how tocombine the two and anyhelp in the right direction would be much aprechiated.
Edit Soloution
thank you for the help I went with declaring the string and linking to it in the query for anyone that needs something simple this is my final code for it
public Cursor fetchAll() {
String OrderBy ="case Day"+
" when 'Monday' then 0 "+
" when 'Tuesday' then 1"+
" when 'Wednesday' then 2"+
" when 'Thursday' then 3"+
" when 'Friday' then 5"+
" end";
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_MODULECODE,
KEY_DAY, KEY_TIME, KEY_DURATION, KEY_TYPE, KEY_ROOM},null, null, null, null,OrderBy );
}
If your Day values are the day names then you want something like this:
order by case Day
when 'Sunday' then 0
when 'Monday' then 1
when 'Tuesday' then 2
...
when 'Saturday' then 6
end
in the SQL; keep in mind that you can hand an SQL ORDER BY pretty much any expression. The orderBy argument to query can be any SQL ORDER BY clause (without the order by) so you want this big ugly string :
"case Day when 'Sunday' then 0 when 'Monday' then 1 ... when 'Saturday' then 6 end"
as the last argument to query. You'll have to properly fill in the ... of course.
If you cannot figure out how to pass a complex query through SQLiteDatabase.query(... lots of variables...) just pass your select statement as a string with SQLiteDatabase.rawQuery().
For example:
String query = "select _id, busnum, " +
" case cast cast (strftime('%w', servdate) as integer) " +
...
String[] selectionArgs = new String() { yada, yada, yada }
mDb.rawQuery(query, selectionArgs);
Related
How would I write a query that selects where the selection is IN the column? I've only seen it where the column is in the selection like this:
String[] names = { "name1", "name2" }; // do whatever is needed first
String query = "SELECT * FROM table"
+ " WHERE name IN (" + makePlaceholders(names.length) + ")";
Cursor cursor = mDb.rawQuery(query, names);
I basically want the opposite of this.
My app is a recipe finding app so there is one column that has a string of ingredients for every entry. I want to be able to select the recipe where the input selection (let's say "chicken rice") was in the string of ingredients ("chicken rice onions...").
Is it possible to do this?
this is what my query looks like now
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {COLUMN_ROWID,
COLUMN_NAME, COLUMN_TYPE, COLUMN_INGRED, COLUMN_SPECIAL, COLUMN_DESCRIPT, COLUMN_ALLINGRED, COLUMN_INSTRUCT, COLUMN_IMGPATH},
COLUMN_ALLINGRED + "like '%" + inputText + "%'",
null, null, null, null, null);
Is your second query not working? You're not doing a "selection in column", it's just a simple text search. But it has many issues (besides performance). What if your ingredients are in a different order? For ex. rice chicken. You probably want to store each ingredient on its own and tokenize inputText and search for all matches of either of the tokens, and prioritize both. Text search is not easy, you'd be much better off offloading this to a service and making calls from the client. You shouldn't have so much offline data anyway, presumably you'll want to update it frequently.
I am querying call dates in call log database in android phone. After reading some articles, they said this needs to make some injection in my query method like this.
Cursor cursor = activity.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE},
"" + ") GROUP BY (" + CallLog.Calls.DATE,
null, CallLog.Calls.DATE + " DESC");
When I run my app and I got the following error message.
near ")": syntax error (code 1): , while compiling: SELECT date FROM calls WHERE ((() GROUP BY (date) AND ((type != 4)))) ORDER BY date DESC
Actually, I can't figure out how this query was built and why ((type !=4)) filter was appended to GROUP BY clause. I am currently using (compileSdkVersion 19, minSdkVersion 10 and targetSdkVersion 19). Is there any way to make raw query to Call Log Database because I want to write the query with my own to group dates.
CursorLoader cl = new CursorLoader(getActivity(), CallsQuery.CONTENT_URI, CallsQuery.PROJECTION, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
You can use the DEFAULT_SORT_ORDER which is date DESC
public static final String DEFAULT_SORT_ORDER
The default sort order for this table
Constant Value: `"date DESC"`
Just remove the extra brackets from your query and change it as below:
Cursor cursor =mActivity.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE},
" GROUP BY " + CallLog.Calls.DATE,null,CallLog.Calls.DATE + " DESC");
I want to use this:
return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_LEVEL }, KEY_LEVEL + ">= 3 AND " + KEY_LEVEL + " < 5", null, null, null, null);
But instead of 5 I want it to check the next column where it finds greater than or equal to three.
So when I give an input of say 5, I want it to check for greater than or equal to 5 in column 3 but less than the value in the next column, column 4.
Original code taken from Sqlite query check - less than and greater than.
Just do this :
String[] selectionArgs = { "5", "5" };
String[] columns = { KEY_ROWID, KEY_COLUMN3, KEY_COLUMN4 };
String selection = "KEY_COLUMN3 >= ? AND KEY_COLUMN4 < ?";
return mDb.query(DATABASE_TABLE, columns, selection, selectionArgs, null, null, null);
For me just passing numerics in String format like #buzeeg offered didn't work.
PROBLEM: My task was to perform UPDATE and in my case it should not have succeed, but as you may guess, when we use 1 as query arg, '1'>2 means true:
UPDATE table SET field = 1 WHERE ? > 2
SOLUTION: CAST helped me to solve it:
SELECT * FROM smth WHERE CAST(? as integer) > 2
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'm trying to get a query that returns rows only with a specified name, and sort descending by week (integer).
Everytime I try and run it it gives me a FC and logcat says
ERROR/AndroidRuntime(728): android.database.sqlite.SQLiteException: no such column: New: , while compiling: SELECT Name, Week, Total FROM notes WHERE Name=New ORDER BY WeekDESC LIMIT 10
public Cursor graphQuery(String name){
return mDb.query(DATABASE_TABLE, new String[] {KEY_NAME,
KEY_WEEK, KEY_TOTAL}, KEY_NAME + "=" + name, null, null, null, KEY_WEEK + "DESC","10");
}
It says that there is no such column, which doesn't make sense because it should be looking at the names in the row and returning the ones that match. Also if I put KEY_NAME + "=" + name as null, it says that there is no such column WeekDESC which doesn't make sense either cause it is just supposed to be sorting by an integer.
I have gotten this code working before, but I misplaced it and can't seem to get it working again... This is getting frustrating, any help is very much appreciated!
If you search by string, you should use a question mark as a placeholder, i.e.
return mDb.query(DATABASE_TABLE, new String[] {KEY_NAME,
KEY_WEEK, KEY_TOTAL}, KEY_NAME + "=?", new String[] { name }, null, null, null, KEY_WEEK + "DESC","10");
Or else it will break if the name contains spaces or special characters.
(If you really don't want to you the question mark, you need to put the string in quotes or double-quotes, but using a question mark is far more elegant and safe.)
Also: There is no space between "DESC" and the key you're sorting by. Use " DESC".