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.
Related
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.
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.
In my application i am trying to fetch records from a sqlite table with respect to a range of dates selected from the date picker. The records in the table are as follows :
The query formed is as given below :
select * from Order_Master where Order_Date >= '12-04-2015' and Order_Date <= '11-03-2016' And WSS_Code = '1014332'
This query does not return any value which is not the desired result as the dates are in the selected range.
What could possibly be wrong here ? Am i missing something?
Kindly guide me through this. Thanking you in Advance !
Your date format cannot be used for comparisons, because strings are compared lexicographically, i.e., with the first characters compared first.
In this query, you are searching for dates with a month that is at least 12 and, at the same, no larger than 11.
SQLite has no separate data type for dates.
To store dates in a database, you have to choose one of the existing data types (number or text).
When using SQLite's built-in date functions, you must use one of the formats supported by them.
Try this , hope it helps
Change your datatype to text
SELECT * FROM Order_Master WHERE Order_Date BETWEEN '12-04-2015' AND '11-03-2016' AND WSS_Code = '1014332';
As well as your query will also work.
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)) ...
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.