How select a period of date from sqlite - android

I have a table in SQLite that has three fields (id, fromDate, toDate, uid). I want to select rows that has created in a specific dates. User select the period of time (from date/ to date). How I can query with ORMLite?

Sqlite does not have a Date type(supported types)
There is some dateTime functions, i think that any of function will meet your requirement... The easier solution will be to convert you date to a timestamp and compare the timestamp.

Related

Sqlite Sort by time not working

I am storing date format in sqlite table, I want sort by date from table.
Every record in my SQLite database contains a column which contains a date stored as a string in the format 'yyyy-MM-dd HH:mm:ss'.
I am sharing my table structure. I am using this query to sort by datetime it not sorting by time but sort by date is working fine.
select *
from messages_table
where id = '444'
order by datetime(date_time) asc
I am storing datetime as string in my below table
and I am getting the below wrong sorting by time output please see my date_time column in the picture, anyone guide me.
I have a similar problem with the TIME datatype.
If I enter time values correctly, such as 8:00 or 13:00, they are sorted as strings in an ORDER BY clause, in effect as "800" and "1300", where the string "1300" is sorted before "800" in ascending order.
One solution is to pad all times with a leading 0, so we get "0800" and "1300" which will be sorted time wise correctly.

Datetime aritmethics in sqlite on android

I am trying to do some datetime aritmethics in Sqllite. I have a table with two rows defined as type DATETIME. These fields are called LOG_FROM and LOG_TO. In a query on this table I try to do the following:
SELECT SUM(LOG_FROM - LOG_TO) AS TIMESPENT FROM LOG_TABLE WHERE X=Y;
This subtraction inside the SUM function does not work as intended. It will always return the number 0. I read the data via a cursor.getString.
When I store dates in the table I convert them to strings on the format yyyy-MM-dd HH:mm:ss
SQLite ignores column data types. Your date strings are strings.
Your date format is supported by the built-in date functions, so you can use them to convert the dates to numbers.
Assuming that you want the result to be a number of days, use julianday:
SELECT SUM(julianday(LOG_TO) - julianday(LOG_FROM)) ...

Android: Save date normally to sqlite but sort by date considering month and day

I have seen number of post about storing date.
I am still not getting the fine and exact approach about saving it to a sqlite database.
I am able to store it, but during sorting I need to consider only month and day just like birthday where years doesn't matter.
What will be the query if I want to get the row whose date is 2 or 3 days in advance, like 2nd march row if searched on 28 Feb?
You should start by checking out the SQLite documentation of date & time functions.
For instance, to solve your problem "And what will be the query if i want to get the row whose date is 2 or 3 days in advance" you'd use julian day calculations, such as this example that you can execute directly in the sqlite3 shell:
create TABLE example (_id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL);
insert into example (date) values ('2011-01-02');
insert into example (date) values ('2011-04-02');
insert into example (date) values ('2012-02-26');
insert into example (date) values ('2012-02-27');
insert into example (date) values ('2012-02-28');
insert into example (date) values ('2012-02-29');
insert into example (date) values ('2012-03-01');
insert into example (date) values ('2012-03-02');
insert into example (date) values ('2012-03-03');
select date from example where julianday(date) - julianday('now') < 3 AND julianday(date) - julianday('now') > 0;
This would return (given that "today" is feb 28th) all the days that are one, two or three days in the future:
2012-02-29
2012-03-01
2012-03-02
Edit: To only return rows, regardless of year, you could do something like this - using a VIEW (again, exampl is directly in SQLite):
create view v_example as select _id, date,
strftime("%Y", 'now')||'-'||strftime("%m-%d", date) as v_date from example;
This VIEW would return the date & times in your database "rebased" on the current year - which, of course could introduce all manner of wonky behavior with leap years.
You can select all the dates like this in that case:
select date from v_example where
julianday(v_date) - julianday('now') < 3 AND
julianday(v_date) - julianday('now') > 0 ORDER BY v_date;
Which would return:
2012-02-29
2012-03-01
2001-03-01
2012-03-02
2010-03-02
If you want to sort by day and month consider storing the date as string in the format ddMMyyyy (you need two digits for day and month, otherwise the sorting will be flawed). Sorting by increasing values will give you dates sorted by day and month (and then year).
You can even do range query with string but you have to compute the query string.
Alternatively you may store the date as milliseconds in an additional column (this is the usual format for dates in the database) and do the range queries more easily with integer arithmetic.
One option is to use strftime() in SQLite to strip of the year and then do a comparison.
select * from sometable where strftime('%m-%d', somecolumn) = '02-28'
This will do a query of all rows for February 28th. But performance might be hurt if you have a large table and need to do a string conversion of every row for comparison. Maybe store the day and month in two additional columns if this query is performed often?

Deleting from android sqlite table

I have a column in android sqlite database table. The values in the column are like:
2011/06/01
2011/06/02
2011/06/05
2011/06/10
2011/06/11
2011/06/13
2011/06/15
2011/06/16
2011/06/25
2011/06/26
I have a string today="2011/06/27"
Now I want to delete those rows whose column value is older than 5 days from today.
How to modify the code to achieve this?
return db.delete(DATABASE_TABLE,where date="", null) > 0;
You have to store your dates in a other format, check the date function of SQLite. Use YYYY-MM-DD instead of YYYY/MM/DD. You already got an order on your dates defined by its string representation. So you can use the date function of SQLite to select the correct rows.
Dates older than 5 days would be queried like this.
... WHERE datecolumn < date('now', '-5 days') ...
There is no date type in sqllite
you just store a data represented value as a string or int
then use the date based functions of sqllite to get the values
You should be using the stored procedure rather than writing the service. It will make easier for your application to automatically update the records.
See link: http://searchoracle.techtarget.com/answer/Time-based-stored-procedure-to-check-table-and-update-old-records
Another solution is to use date instead of string. When you run your service then fetch data as Date and then you can make the simple comparison.

How to search a column in an SQLite database for specific text?

More specifically, the first two columns of my read-only database are the row number (the _id integer primary key), and a date and time in the text format 7/21/2011 HH:MM:SS PM. In my Android app, the user selects a start and end time (HH:MM), and then data about that time interval is displayed graphically. How do I search this second row of my database for the inputted times?
So if your Date Column contains TEXT(since there is no DATE in SQLite), you should use the SQLite function
strftime(format, timestring, modifier, modifier, ...)
See docs SQLite Date functions
So you SQL query may look something like this:
SELECT * FROM mytable WHERE strftime('MM-DD-YYYY HH:MM:SS', date_column) < strftime('MM-DD-YYYY HH:MM:SS', '7/21/2010 22:00:00')
where the last date parameter will be the inputted date.
I am not familiar with SQL time commands. If you select all rows and then process them in the java code using Date objects and their .compareTo() methods you can only display rows of interest to your user.

Categories

Resources