Skip to content Skip to sidebar Skip to footer

Sqliteexception Using Where " +key_date+"='"+date+"'"

This is on my selectedDayChange on my mainactivity.java date= year +''+ month +''+ dayOfMonth; allfood food = new allfood(); food.Date='DAT

Solution 1:

1) Don't add a literal allfood object to a String. SQL can't interpret a Java object.

The method should be any of the following because allfood is the whole object, you do need it as the parameter. And naming it as date is simply confusing.

  • totalFat(Date date)
  • totalFat(String date)
  • totalFat(Calendar date)
  • totalFat(int year, int month, int dayOfMonth)

should be Date=DATE_20170213

2) No, it really shouldn't because Sqlite does not support that format of dates. Additionally, pre-pending DATE_ is just wasting storage in your database.

3) Please do not use this

date=year+""+month+""+ dayOfMonth

Build a Calendar object and use SimpleDateFormat to correctly get a date formatted string.

using the last option above, you'd have something like this

SimpleDateFormatfmt=newSimpleDateFormat("yyyy-MM-dd");
Calendarcalendar= Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
StringqueryForDate= fmt.format(calendar.getTime());
// db.query(TABLE_NAME, null, new String[] {...  // TODO: Complete this 

Post a Comment for "Sqliteexception Using Where " +key_date+"='"+date+"'""