user555910
user555910

Reputation: 2667

I want to take data from database and compare with system time

I want to take data from database and compare with system time.my android code for taking system time is

sTimeFirstPart= DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());

this will hold the current system time and how can i compare this time to time in database table my db table is look like this

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS names3 (_ID INTEGER PRIMARY KEY     AUTOINCREMENT, medicine VARCHAR, dose1 VARCHAR ,dose2 VARCHAR ,dose3 VARCHAR)");

dose1,dose2,dose3 are the time value stored in the database.

and i want to display the next medicine at what time how can i do this pls help me?

Upvotes: 1

Views: 1167

Answers (3)

pawelzieba
pawelzieba

Reputation: 16082

This example resolves that problem:

SELECT * FROM my_table WHERE 
  start_date <= date('now','localtime') 
  AND end_date >= date('now','localtime')

Look here for other date functions in SQLite.

Upvotes: 0

drstupid
drstupid

Reputation: 433

I'm not an Android dev, but comparing dates as strings is a bad idea on any platform. Why don't you use Date instead of String? You can transform the Varchar to a Date like this and then compare it to the current time with compareTo.

Upvotes: 1

Heiko Rupp
Heiko Rupp

Reputation: 30974

You should consider storing the date as Millis since the epoch -- what System.currentTimeMillis() returns.

Then add a column "theTime" as long to the table and store the next intake date there.

Here comparison using < and > is easy:

SELECT dose FROM names3 WHERE theTime < ?

and pass the current time as parameter.

Upvotes: 2

Related Questions