I'm working on an app where Dates are entered in the Room database as Strings, with a SimpleDateFormat fixed to '%d/%m/%Y %H:%M:%S' (e.g. "26/12/2018 10:33:31").
Without changing the structure of the database, I'd like to query some results, ordered by date. This should probably look like this (inside a Dao):
#Query("SELECT * FROM results ORDER BY stringToDate(startTime) DESC LIMIT :count")
List<Results> getLast(int count);
Unfortunately, this throws a compile error: "no such function: stringToDate", no matter if the function exists, even if it is annotate as TypeConverter in due form.
Moreover, since String is accepted as a Room type, I'm not event sure I could use TypeConverter here to begin with.
Is there any way I could order by Date as String without modifying the database structure ?
Try:
#Query("SELECT * FROM results ORDER BY strftime('%Y-%d-%m-%Y', startTime) DESC LIMIT :count")
Make sure that the format passed to strftime match your time format.
For reference formats can be found here:
https://www.tutorialspoint.com/sqlite/sqlite_date_time.htm
Can you try changing stringToDate(startTime) to datetime(startTime)?
Can you provide a sample of startTime object?
Related
So I'm trying to query all the same dates but this code won't work
SELECT * FROM schedTBL WHERE CONVERT(varchar, DueDateTime, 120) LIKE " + "'%"+ text + "%'"
it returns no such column:varchar
This looks like a SQL statement for SQL Server, not SQLite...
Assuming you are storing dates in field DueDateTime without the hour part, something like this should do:
SELECT * FROM schedTBL WHERE DueDateTime = '2016-09-24'
Or you'll need like if you also have hour values. That depends on how you are actually storing dates/times in your SQLite DB.
Try formatting the date in Java and send it as a parameter to your query.
SQLite doesn't handle dates like SQL Server.
See:
SQLite Date And Time Functions
In SQLite, date values would be formatted with the strftime() function, or for this particular format, with the date() function:
... WHERE date(DueDateTime) LIKE ...
But this works only for supported date formats.
If the values in your database use an unsupported format, your only choice is to convert them, or to try to match the actual string:
... WHERE DueDateTime LIKE ...
I have an SQLite database within my Android application, which stores dates as integers. These integers are derived from a call to Java.util.Date.getTime();. I am trying to run a raw query of my database to get a Cursor to pass to a CursorAdapter and display in a ListView, but the date is stored as an integer as returned by getTime().
To keep my program simple, I would like to avoid using a SimpleArrayAdapter, and stick with the CursorAdapter.
Is it somehow possible to format the integer within the date colum as mm-dd-yyyy so that the column of the table, that the cursor is pointing to, contains properly formatted values rather than the integer that was returned by Java.util.Date.getTime(); when I added the item to the database?
SELECT strftime("%m-%d-%Y", date_col, 'unixepoch') AS date_col
Your code will work if it expects a result set column in that format called date_col.
EDIT: One thing you need to watch out for is that getTime uses milliseconds since 1970, while standard UNIX time (including SQLite) uses seconds.
The Java.util.Date.getTime(); method is returning an integer that represents the "unix time".
The simplest way to read this number as a date is by storing it as-is, and reading it using the following Sqlite query:
SELECT strftime('%m-%d-%Y', 1092941466, 'unixepoch');
which returns:
08-19-2004
If you need another format, you can use the strftime function to format is as you like, or any of the other date formats and functions available.
You'll have to, as Matthew Flaschen points out in a commend below, divide the date by 1000 before you are able to use them in this way. "Real" unix times are measured in seconds since the epoch, and Java.util.Date.getTime(); returns milliseconds since epoch.
SQLite uses static rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
Any value stored in the SQLite database has one of the following storage class:
NULL
INTEGER
REAL
TEXT
BLOB
so I am not sure what you meant by but the date is stored as a long, unhelpful integer.
For more details please refer to Datatypes In SQLite Version 3. For further information on storing date/time in SQLite please refer to SQL As Understood By SQLite.
I hope this helps.
The time format in my table is like this : 02:59:00 (hh:mm:ss).
Have been trying to make the query like this:
SQL = "select * from Events Where date = '"+tomorrowDate+"'
AND city = '"+mCity+"'
order by strftime('%H:%M:%S',start_time) ASC;";
However the time order still seems random. What am I doing wrong?
Looking at the docs, strftime is used to convert datetime to the specified format. You should be able to accomplish the ordering with just:
order by time(start_time) asc;
or
order by datetime(start_time) asc; # if date is included
without needing to convert the datetime format.
Using the greenDAO generator i use:
entity.addDateProperty("date").notNull();
I insert the value: '2013-10-30', and when i check on my database i have the value there.
I checked this value with this query:
select datetime(date) from myTable;
and it returns:
2013-10-10 00:00:00
now in android, when i try to retrieve this value i do (for each row):
myTable.getDate();
which always returns 01/01/1970.
What i found (but might not be the cause for this problem):
I did some debug in my code, and i found out that the class myTableDao.java, generated by greenDAO, on method public void readEntity(Cursor cursor, History_Job entity, int offset) is returning the value "2013" from the cursor:
entity.setDate(new java.util.Date(cursor.getLong(offset + 1)));
Still inside myTableDao.java, method createTable() i have this field declared as Integer.
...
"'DATE' INTEGER NOT NULL ," + // 1: date
...
Am i doing something wrong with the property DATE, or is it a bug on greenDAO when trying to use this type of field?
GreenDao uses Java Date objects in the Entities and stores their Date.getTime() (time-in-millis-since-epoch) value as a SQLite INTEGER. This works just fine both ways if you use it properly. I'm not sure why you're manually inserting dates - that defeats the purpose of using an ORM library.
GreenDao always returns Date in that particular format. You can call it a bug.
You can see the code here
https://github.com/greenrobot/greenDAO
What you can do is
fork GreenDao from here
Add a method setDateFormat(String format) and also contributes to others.
See doc of SQLite Date Datatypes here.
SQLite stores date in three formats TEXT,REAL and INTEGER so its all about the GreenDao method which is returning date to you. You can make it modify.
I have an SQLite database within my Android application, which stores dates as integers. These integers are derived from a call to Java.util.Date.getTime();. I am trying to run a raw query of my database to get a Cursor to pass to a CursorAdapter and display in a ListView, but the date is stored as an integer as returned by getTime().
To keep my program simple, I would like to avoid using a SimpleArrayAdapter, and stick with the CursorAdapter.
Is it somehow possible to format the integer within the date colum as mm-dd-yyyy so that the column of the table, that the cursor is pointing to, contains properly formatted values rather than the integer that was returned by Java.util.Date.getTime(); when I added the item to the database?
SELECT strftime("%m-%d-%Y", date_col, 'unixepoch') AS date_col
Your code will work if it expects a result set column in that format called date_col.
EDIT: One thing you need to watch out for is that getTime uses milliseconds since 1970, while standard UNIX time (including SQLite) uses seconds.
The Java.util.Date.getTime(); method is returning an integer that represents the "unix time".
The simplest way to read this number as a date is by storing it as-is, and reading it using the following Sqlite query:
SELECT strftime('%m-%d-%Y', 1092941466, 'unixepoch');
which returns:
08-19-2004
If you need another format, you can use the strftime function to format is as you like, or any of the other date formats and functions available.
You'll have to, as Matthew Flaschen points out in a commend below, divide the date by 1000 before you are able to use them in this way. "Real" unix times are measured in seconds since the epoch, and Java.util.Date.getTime(); returns milliseconds since epoch.
SQLite uses static rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
Any value stored in the SQLite database has one of the following storage class:
NULL
INTEGER
REAL
TEXT
BLOB
so I am not sure what you meant by but the date is stored as a long, unhelpful integer.
For more details please refer to Datatypes In SQLite Version 3. For further information on storing date/time in SQLite please refer to SQL As Understood By SQLite.
I hope this helps.