I’m trying to insert the values from a date picker and a time pickers in my sqlite database. I have stored the date and time in object variables and then call a method in my DBhandler class to insert them through a contentvalue object.
Now where I’m failing here is with the actual insertion and data types.
I’m getting an error on the following code for the ‘timeApp’ and ‘dateApp’ I’m passing these variables to the createAppointmentEntry’ method as DATE and TIME variables.:
Error message:
The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Time)
Method the errors occuring on:
public void createAppointmentEntry(String nameApp, String typeApp, Time timeApp, Date dateApp ,String commentApp, Boolean onOrOff) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAMEAPP, nameApp);
cv.put(KEY_TYPEAPP, typeApp);
//ERROR on the following two 'puts'
cv.put(KEY_TIMEAPP, timeApp);
cv.put(KEY_DATEAPP, dateApp);
cv.put(KEY_COMMENTAPP, commentApp);
cv.put(KEY_ALARM, onOrOff);
ourDatabase.insert(DATABASE_TABLE, null, cv);
Heres my DB columns:
public static final String KEY_ROWAPPID = "_appid";
public static final String KEY_NAMEAPP = "app_name";
public static final String KEY_TYPEAPP = "app_type";
public static final String KEY_TIMEAPP = "app_time";
public static final String KEY_DATEAPP = "app_date";
public static final String KEY_COMMENTAPP = "app_comments";
public static final String KEY_ALARM = "app_alarm";
My onCreate method:
db.execSQL("CREATE TABLE " + DATABASE_TABLEAPP + " (" +
KEY_ROWAPPID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAMEAPP + " TEXT NOT NULL, " +
KEY_TYPEAPP + " TEXT NOT NULL, " +
KEY_TIMEAPP + " TEXT NOT NULL, " +
KEY_DATEAPP + " TEXT NOT NULL, " +
KEY_COMMENTAPP + " TEXT NOT NULL, " +
KEY_ALARM + "BOOLEAN NOT NULL);"
);
Heres how I am setting the time and date object variables:
Date setDate = new Date(dobYear - 1900, dobMonth, dobDate);
Time timeToSet = new Time();
timeToSet.set(0, dobMinute, dobHour);
Judging from the compilation error and the API of
ContentValues, it seems thatContentValuesonly supports primitive types,Stringandbyte[]. So, try replacing:with:
You can use formatter to format the
TimeandDateto a standardStringto be able to parse it back.