android - Select some data from SQLite using a date filter? -
i want select data database table using filter date (date column is: [searchdate] datetime default current_timestamp,)
in pic, i'm using sqlite expert , filter date:
and code getting current year , month:
dateformat year_format = new simpledateformat("yyyy"); dateformat month_format = new simpledateformat("mm"); year_date = year_format.format(new date()); month_date = month_format.format(new date()); date date = new date(); log.d("month", year_format.format(date) + " " + month_format.format(date));
and cursor query in app getting list filtered date (current year):
public list<moment> moment_year(string year) { try { moment moment_table; list<moment> moments = new arraylist<>(); db_helper.opendatabase(); db.begintransaction(); cursor moment_cursor = db.rawquery("select * tblmoment strftime('%y', searchdate) =" + year, null); db.settransactionsuccessful(); moment_cursor.movetofirst(); while (!moment_cursor.isafterlast()) { moment_table = new moment(moment_cursor.getint(0), moment_cursor.getstring(1), moment_cursor.getstring(2), moment_cursor.getstring(3), moment_cursor.getstring(4), moment_cursor.getstring(5), moment_cursor.getint(6), moment_cursor.getint(7) != 0); moments.add(moment_table); moment_cursor.movetonext(); } moment_cursor.close(); return moments; } catch (exception e) { return null; } { db.endtransaction(); db_helper.closedatabase(); } }
i tried, nothing happens , list null (empty).
have dates in current year.
isn't clear? surround year
variable sql string delimiters ('
) in query.
cursor moment_cursor = db.rawquery("select * tblmoment strftime('%y', searchdate) = '" + year + "'", null);
or use parametric query (the string delimiters handled android).
cursor moment_cursor = db.rawquery("select * tblmoment strftime('%y', searchdate) = ?", new string[]{year});
Comments
Post a Comment