SQLite date - find event from today - android

I would like to retrieve from a SQLite database a list of events which have their start_time "today". start_time contains a timestamp e.g. like this 1338613200000.
I tried this:
SELECT * FROM events WHERE date(start_time, 'unixepoch')=date('now')
Doesn't work...
I achieved the same thing in MySQL with such a statement:
SELECT * FROM events WHERE DATE(start_time)=CURDATE();
EDIT:
It's the problem with my timestamps. They have three zeros too much

Java uses milliseconds since the Unix epoch, whereas I'm guessing SQLite uses seconds.
Divide System.currentTimeMillis() by 1000 before querying the database.

You can try something like this:
SELECT * FROM events
WHERE datetime(start_time,'unixepoch', 'localtime')
BETWEEN DATE('now') AND DATE('now', '+1 day')
See more details in here

Try
Calendar c= Calendar.getInstance();
long l=c.getTimeInMillis();
and do something like the following:
SELECT * FROM events WHERE start_time=l;
see if that works.

Related

Android SQLLite last 31 days data from current_timestamp column

I am trying to pull last 31 days data from SQLLite db, using below SQL. I already searched google and tried various options, while all SQL works i am not getting results as expected. For example in below screenshot you will see i do have record on 18-Sep but sql doesn't return any results...I am not sure what i am missing here..
SELECT * FROM transactions WHERE TIMESTAMP > (SELECT DATETIME('now', '-30 day'))
Use between instead of Timestamp comparison directly -
SELECT * FROM transactions WHERE TIMESTAMP BETWEEN datetime('now', '-31 days') AND datetime('now', 'localtime')
This is because SQLlite stores dates in String format and doesn't have a standard DateTime format. So The direct comparison of String would fail. Hence using of the datetime function to compare dates with existing values helps.
You can also try as
SELECT * FROM TRANSACTIONS WHERE TIMESTAMP > datetime('now', '-30 days')

Android working with dates and SQLite

I am trying to create a simple reminder app. Im just logically thinking about the process. Basically I want to be able to choose a DAY and time e.g Monday 15:00, this will trigger EVERY Monday 15:00 until it gets deleted from database. Having said that example I have questions to accomplish this process.
How will I store DAY and TIME, what type, do I need different columns in my table?
How can I compare real time DAY to current DAY, so if its Monday real time it will return ONLY Monday reminders? is this possible?
Will I need to primarly focus using calendar?
As documentation says:
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values.
You can store date and time in the TEXT type of field in the following format:
YYYY-MM-DD HH:MM:SS.SSS
and then use built-in date & time functions of SQLite to filter your records.
Another option is to store date & time as a time-stamp in milliseconds and write proper queries for selecting data or use Joda library for filtering or date & time transformation, but probably such solution would be less efficient and less convenient than first option.
Using integer column is the easiest solution.
Just store the date in millisecond (Calendar.getTimeInMillis()) and your good to go.
Then you just have to search on that integer to find the correct event in your database :
String selectQuery = "SELECT whateveryouneed FROM events WHERE date_event > ?";
Cursor c = db.rawQuery(selectQuery, new String[] { String.valueOf(calendar.getTimeInMillis())});
...
if you need to find all the event for a day , you just have to find the limits of the day in millisecond and make a query according to those limits

Sqlite get records by hour

I want to make a query to get records between specified hours. For example, i want to get all records between 00:00 and 01:00 for all days. So, the date does not matter but hours. How to do that?
I have done this, but it only return for certain dates.
Select name from my_table where date_column> beginning and date_column< end
Here beginning and end are in millisecond. Also my date_column is stored in millisecond format.
Use strftime():
Select name
from my_table
where strftime('%H', date_column) = '00';
This just checks the hour. You could use '%H:%M:%S' if you wanted more granularity.
EDIT:
You do not have a date time value. You have something else. It looks like a Unix epoch time measured in milliseconds rather than seconds. If so, the following should work:
Select name, datetime(date_column/1000, 'unixepoch')
from my_table
where strftime('%H', datetime(date_column/1000, 'unixepoch')) = '19';
However, none of the times are at hour 3. You may need to convert using your localtime.

Delete where date is 30 days back from now SQL Android [duplicate]

What are the sqlite equivalents of INTERVAL and UTC_TIMESTAMP? For example, imagine you were "porting" the following SQL from MySQL to sqlite:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time > utc_timestamp() - INTERVAL 600 SECOND;
datetime('now') provides you the current date and time in UTC, so is the SQLite equivalent of MySQL's UTC_TIMESTAMP().
It may also be useful to know that given a date and time string, datetime can convert it from localtime into UTC, using datetime('2011-09-25 18:18', 'utc').
You can also use the datetime() function to apply modifiers such as '+1 day', 'start of month', '- 10 years' and many more.
Therefore, your example would look like this in SQLite:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time > datetime('now', '-600 seconds');
You can find more of the modifiers on the SQLite Date and Time Functions page.
There's no native timestamp support in sqlite.
I've used plain old (64-bit) integers as timestamps, representing either micro- or milliseconds since an epoch.
Therefore, assuming milliseconds:
SELECT mumble
FROM blah
WHERE blah.heart_beat_time_millis > ? - 600*1000;
and bind system time in milliseconds to the first param.
there is LOCAL_TIMESTAMP in SQLite, but it's GMT.

SQLite use year with date functions in where clause and group by

I am keeping date time values(Unix timestamps) in a "NUMERIC" format in SQLite table, the value is calculated through Java Date function:
new Date().getTime();
The values look like proper date/time when I load these in an Android program, but when I try to experiment with queries through SQLite data browser 2 beta the results are awkward. Numeric values are given below:
1391313058888
1391313104336
1391313175752
When I try to apply date function the SQLite data browser shows following for all three rows:
-1413-03-01 13:07:12
My query is
SELECT date(trxDateTime, 'unixepoch') from trx_log
I was trying to figure out how to get correct date values in the first place, once I got those then I believe I could figure a way to use it in where clause or group by.
Basically I am trying to show totals sales by year. Any help will be appreciated.
Your times are in milliseconds. Just convert them to seconds, and you'll be fine:
SELECT date(trxDateTime / 1000, 'unixepoch') from trx_log

Categories

Resources