Running Android 4.1.2.
The following should work according to SQLite documentation.
String sql = "SELECT strftime('%m', MyDate) FROM MyTable";
Cursor c = database.rawQuery(sql);
c.moveToFirst();
String s = c.getString(0); // returns null
However, the returned data is always null. If I change the query to SELECT MyDate FROM MyTable then it does indeed return a valid date so I can assure you that my database contains correct data.
Q: Am I facing a bug in the rawQuery method or have I done something wrong?
Related
Cursor cursor = db.rawQuery("SELECT * FROM expenses WHERE expense_date > (SELECT DATE('now', '-7 day'));", null);
I have this above query to get all records that inserted in last 7 days. But as a result of this query i am getting all the records. The date column of these records is inserted via Android's DatePicker and they are in 30-Jul-2015 format. I guess that's why this query does not work properly. Is there a way to make them consistent to get this query work properly or i am missing out something else ? Any help would be appreciated.
I've never tried using the SQLite built in date functions. Taking a quick look at the SQLite date function documentation here, it would seem that to use them the date needs to be in the format YYYY-MM-DD, which would be why your query is failing.
You can do some string manipulation/formatting before saving to the db and use that format.
Personally, I usually format in that manner but without the dash separators, then a simple less than/greater than comparison can be used, treating the date like a regular number. Of course some conversion back and forth is necessary for display, but for under the hood it works great.
int now = 20150730;
String query = "SELECT * FROM expenses WHERE expense_date > '" + now-7 + "'";
I am trying to update a DATETIME column in a Sqlite table using the following query.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.rawQuery(query, null);
I have tested this on both device and emulator but its not working, its not updating the value in the column.
PS: I tried the same query in Sqlite browser and its working there.
Please help me figure out a solution.
Thanks.
Instead of using rawQuery(), use execSQL() if the execution doesn't return any data.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.execSQL(query);
Though just a tip, it is better to use UNIX time if you really want to store and process date in the future. It will be more flexible and standardized.
I am using the following query to get the list of item
Query="Select * from Item_List where idSubcategory = (Select _id from SubCategory_List where Name = ?)";
c = db.rawQuery(Query, new String[] { Search });
But it is returning no result.
I ran this query on the database which I pulled from the device as well as emulator. There I am getting proper result, But when I run it on device no data is returned.
Can you try a different way? Use execSQL() as explained in this link. It is exactly the same problem.
EDIT
Unfortunately i didn't notice that execSQL() return no data (the work day is ending, finally!). The only solution i have is to do a first query and the result of this one will be the parameter of second query.
EDIT 2
Try to add "IN" to your query, like this:
Query = "Select * from Item_List where idSubcategory in (Select _id from SubCategory_List where Name = ?)"
Im using SQLite as data storage for my Android App. I have created one table with column of type datetime. When I do the insert of records or selects statements I use the format dd.MM.yyyy (10.08.2012) for dates.
Now, I have issue with getting the row with the latest / MAX date. I have tried using the MAX(date_column) statement, but it returns wrong date back. I have following dates in my table and it returns 31.07.2012 in stead of 04.08.2012. Anyone know what I'm doing wrong?
04.08.2012
03.08.2012
02.08.2012
01.08.2012
31.07.2012
30.07.2012
Here is part of the code:
String selection = "measurementDate = (SELECT MAX(measurementDate) FROM Measurements)";
Cursor cursor = database.query("Measurements", allColumns, selection, null, null, null, null);
cursor.moveToFirst();
...
Could it be because of 31?
Try to see if this helps:
SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1
It's probably due to the comparison being made on the stored string rather than treating it as a date.
You could store the string in differently, YYYYMMDD, which should compare them correctly or, if you can modify the table, store the dates as milliseconds and format back to a string when needed.
This might help
I have this query in SQLite that I want to return a value based on the date passed. The date is in the database as a String. I know that there is data in the database with that date, but it just returns nothing. I dont know why. When I make the query without comparisson parameters, it returns all the data normally, but using the WHERE clause in the query method, it stops returning. Maybe its just a matter of the SQL syntax. Anyways, here is the statement:
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "current_date = ? ", new String[] {date}, null, null, null);
EDIT
I've made this query in SQLite Database Browser, and it doesn't return any values. Even if its there.
I've seen that this is quite funny and confused. I've tested on Database Browser and also on phone, when I pass the current day, the 'Today' day, it gets the data, but when I pass another day that isnt today, it returns nothing. Very, very weird.
And in Database Browser, I've selected the column that contains the date and it returns only the 'Today' date, even if before there are another dates.
if your date is stored in database in format 'dd-mm-yyyy',then try using it like-
Calendar cal=Calendar.getInstance();
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "date = ? ", String.format("%1$td-%1$tm-%1$tY",cal), null, null, null);
For other formats,please refer http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
The column name was 'current_date' and this is a SQLite function name. So it didn't get the other dates, just the current date.