2015-11-04 57 views
0

我對Android的使用SQLite本地數據庫,我使用的Android活躍,我的表POJO是這樣的:今天怎麼來查詢源碼

@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; 

我的問題是,我查詢今天不工作,或者這裏有一些我想念的東西。

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轉化爲timeInMilliseconds(糾正我,如果我錯了),默認的Android活躍Date對象,如何獲得今天的查詢?

回答

1

您的查詢應該是這樣的:

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

的SQLite允許選擇這樣的當前日期,時間和時間戳:

select current_date, current_timestamp, current_time from <table> 

上述查詢的輸出將是這樣的:

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

如果您要將日期存儲爲整數或unix時期,請執行以下操作: 1.獲取JAVA中的當前時間(以毫秒爲單位)。比方說,它是current_time_in_mill

然後使用下面的查詢

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

例試試這個:select date(1092941466, 'unixepoch')

+0

在我的SQLite使用的時間是INTEGER,我不認爲這回答我的問題。在我看來,我的問題與現在在sqlite中將日期轉換爲系統當前時間毫秒有關。 – david

+0

關鍵問題是:你在桌子上保存了什麼?你保存爲整數/毫秒/時代? - 如果是,那麼請參閱我的新查詢3 如果您將日期保存爲SQLite中的默認格式YYYY-MM-DD,那麼上述查詢將起作用。 –

+0

active android自動保存Object Date爲毫秒。正如我的Table Pojo所代表的那樣。所以它以毫秒爲單位。 – david