pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

strftime() in ActiveAndroid #533

Open deshario opened 7 years ago

deshario commented 7 years ago

How to use strftime() in ActiveAndroid ! I'm gonna select records by specific dayofmonth or month or year ... but i have no idea that why where clause isn't working !

public static List<Records> getAllByYear() { 
            return new Select()
            .from(Records.class)
            .where("strftime('%Y',data_created) = "+2017) // Not Returning any data
            .execute();
  }
saulojg commented 7 years ago

Try with:

public static List<Records> getAllByYear(int year) { 
  Calendar cal = Calendar.getInstance();
  cal.set(year, 0, 1, 0, 0, 0);  
  return new Select()
    .from(Records.class)
    .where("data_created >= ?", cal.getTimeInMillis())
    .execute();
}
deshario commented 7 years ago

data_created in my table isn't timestamp format. This is a format of data_created : 2017-08-18 It seems to be not working. I need to use where clause strftime() by year or month or dayofmonth. Ex : select * from tablename where strftime('%Y-%m', data_created) = '2017-08' (select by month or year)