2

I have a table in SQLite database is called message the message table contains message_date column. I need the data that was inserted 7 days ago. I can't compare the Date because the message_date is stored as a millisecond long value my writen query was :

 Cursor cursor = db.rawQuery("SELECT csId FROM table_messages WHERE date < date('now','-7 days') ", null);

anyone can correction my query

Abaneel Jahid
  • 135
  • 10

2 Answers2

0

You can get millisecond value from Date().getTime() or System.currentTimeMillis() both will return "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC."

To get seven days previous timestamp. try this -

Calendar current = Calendar.getInstance();
current.add(Calendar.DATE, -7);
long millis = current.getTime().getTime();//Change this line

Cursor cursor = db.rawQuery(
    "SELECT csId FROM table_messages WHERE date < " + String.valueOf(millis), null);  

EDIT : You see second line of code there you can add subtract days as you wish and extract milliseconds from calender object and use it in your query

EDIT 2 :
1. current.add(Calendar.DATE, -7); Here -7 means you are subtracting 7 days from today. You can make it -30(Subtract 30 days) or just 30(add 30 days) means try changing value because may be table you r querying doesn't have any records matching the condition.

2.Select all rows from table and print it on logcat and print milliseconds you got from Calendar, manually match that milliseconds you got from calendar exist in table or not.

3.Replace this line long millis = current.getTimeInMillis(); with long millis = current.getTime().getTime();

4.If you are getting an error, post error message

If this solves your problem then please mark this post as answer.

Rahul
  • 675
  • 6
  • 18
  • your submitted query will work for 7 days ago data ? @Rahul – Abaneel Jahid Jul 11 '17 at 03:39
  • do you understand my question. I am searching a query that will find the inserted record that was inserted 7 days ago. – Abaneel Jahid Jul 11 '17 at 04:07
  • the latest query does not returen anything – Abaneel Jahid Jul 11 '17 at 04:16
  • You can't just say "Give me records inserted seven days ago" to SQLite. You said your sqlite database stores date as miliseconds and I give solution to get miliseconds and compare it. Please post your table data them may be I can help you – Rahul Jul 11 '17 at 04:26
  • sorry . I have the table called table_message. this table contains 4 column msg_id, content, date, csid, I need the record that was sent more then 7 days ago. I can't give you simple data cz i don't have any rooted device. the date coulmn is a integer column thats contain time in milisecond – Abaneel Jahid Jul 11 '17 at 04:31
0

You can query with the timestamp > 6 days ago and < 7 days ago

Calendar sixDaysAgo = Calendar.getInstance();
        sixDaysAgo.add(Calendar.DATE, -6);
        long millis6Days = sixDaysAgo.getTimeInMillis();

        Calendar sevenDaysAgo = Calendar.getInstance();
        sevenDaysAgo.add(Calendar.DATE, -7);
        long millis7Days = sevenDaysAgo.getTimeInMillis();

        Cursor cursor = db.rawQuery(
                "SELECT csId FROM table_messages WHERE date > " + String.valueOf(millis6Days) + " AND date < " + String.valueOf(millis7Days), null);
Khang Tran
  • 467
  • 5
  • 16
  • I have the table called table_message. this table contains 4 column msg_id, content, date, csid, I need the record that was sent more then 7 days ago. I can't give you simple data cz i don't have any rooted device. the date coulmn is a integer column thats contain time in milisecond – Abaneel Jahid Jul 11 '17 at 04:32
  • 1
    Does your date column saved in timestamp format ? Could you query all and print it as log to see what type of it and then will compare exactly. More than 7 days is just compare value of date > milisecond7Days – Khang Tran Jul 11 '17 at 04:36
  • yes date column saved in timestapm formate like : 933424234 like this . – Abaneel Jahid Jul 11 '17 at 04:38
  • See this vote, I think it useful with your case: https://stackoverflow.com/questions/2930768/how-to-compare-sqlite-timestamp-values?answertab=votes#tab-top – Khang Tran Jul 11 '17 at 04:41