how to query today in sqlite

给你一囗甜甜゛ 提交于 2020-01-17 06:04:33

问题


I have a local database in android using sqlite , i used active android and my table pojo is like this:

@Column(index = true, unique = true , onUniqueConflict = Column.ConflictAction.REPLACE)
public long taskId;
@Column( index = true)
public String taskIdno;
@Column()
public String taskDescription;
@Column()
public String taskTypeId;
@Column()
public String customerName;
@Column()
public String customerContact;
@Column()
public String address;
@Column(index = true)
public int status;
@Column()
public Date startSchedule;
@Column()
public int first_visit;
@Column()
public String coordinates;
@Column()
public int radius;
@Column()
public long location_type_id;
@Column()
public long duration;
@Column()
public long taskType_id;

My problem is that my query for today doesnt work, Or there is something im missing here.

  public static List<AATask> allTaskOrderedByDate(String asc_desc){
        From from = new Select().from(AATask.class).where("strftime('%Y-%m-%d', startSchedule ) = strftime('%Y-%m-%d' , DATE('now'))").orderBy("startSchedule ".concat(asc_desc));
        Log.w(TAG , from.toSql());
        return from.execute();
    }

startSchedule is converted to timeInMilliseconds(correct me if im wrong) default to active android Date object, How do I get today query?


回答1:


Your query should be like this :

select * from <table> where <date_col> = current_date

SQLite allows selecting current date, time and timestamp like this :

select current_date, current_timestamp, current_time from <table>

The output of above query will be like this :

"2015-11-04","2015-11-04 04:08:47","04:08:47"

If you are storing date as integer or unix epoch then do the following : 1. Get the current time in milliseconds in JAVA. Lets say it is current_time_in_mill

Then use the following query

select date(current_time_in_mill, 'unixepoch') from <table>;

Example try this : select date(1092941466, 'unixepoch')




回答2:


If you are storing your date as INTEGER, try this:

select * from my_table where date(dateColumn) = date('now');


来源:https://stackoverflow.com/questions/33513353/how-to-query-today-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!