1

I'm currently working on Android studio and trying to insert a date (using a DatePickerFragment) in a local DataBase but I keep on getting an error that the column doesn't exist. I'm a bit of a noob so please be gentle :)

android.database.sqlite.SQLiteException: no such column: date (code 1): , while compiling: SELECT nome, descricao, date FROM exames

I have the DatePickerFragment Class:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private Calendar dateTime=Calendar.getInstance();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    // Do something with the date chosen by the user
    TextView et1= (TextView) getActivity().findViewById(R.id.theDateET);
    et1.setText("Year: " + view.getYear() + " Month: " + view.getMonth() + " Day: " + view.getDayOfMonth());




    dateTime.set(year,monthOfYear,dayOfMonth);

    int mYear = year;   // Here you can get day,month and year.
    int mMonth = monthOfYear +1;
    int mDay = dayOfMonth;

    ContentValues values = new ContentValues();

    values.put("date",mDay + mMonth + mYear);
   /* values.put("Month",mMonth);
    values.put("Year", mYear);*/

     }
}

And in my InsertActivity I call this method:

 public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

Also in my DataBaseAdapter Class I have this function to insert in the DB

 public long insertExame(String oNome, String aDescricao, String date) {
    ContentValues values = new ContentValues();
    values.put("nome", oNome);
    values.put("descricao", aDescricao);
    values.put("date", date);
   /* values.put("month", month);
    values.put("day", day);*/

    return database.insert("exames", null, values);
}

EDIT: Here is where I create my DataBase:

public class HelpDataBase01 extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "base-dados.db";
private static final int VERSION = 1;
public HelpDataBase01(Context context){
    super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE exames(_id integer primary key autoincrement, nome varchar(40), descricao varchar(40), date varchar(100))");

}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE IF EXISTS Exames");
    onCreate(db);

}

}

Thanks for the help!

  • Share the code where you create the exams table in DB – Roberto Artiles Astelarra Nov 27 '15 at 00:16
  • The error says that the field "date" doesn't exist in the table "exams", but I see that you create it in your sql statement. Could be because that you altered the "CREATE" statement, but never executed it after the table was created with the old scheme. Increment VERSION integer to 2, and run the app. It will recreate the table with the proper structure. – Roberto Artiles Astelarra Nov 27 '15 at 00:22
  • Thanks @RobertoArtilesAstelarra, it worked! BTW, am I using the correct type for date? any advice on my noob code? thanks again my friend! –  Nov 27 '15 at 00:29
  • No problems. I posted it as the answer. Regarding the date type checkout this answer http://stackoverflow.com/a/7363557/889636 or this one http://stackoverflow.com/a/13694823/889636 and decide for yourself what is the most suitable way to do it in your case – Roberto Artiles Astelarra Nov 27 '15 at 00:31

1 Answers1

2

The error says that the field "date" doesn't exist in the table "exams", but I see that you create it in your sql statement. Could be because you'd altered the "CREATE" statement, but never executed it after the table was created with the old scheme. Increment VERSION integer to 2, and run the app. It will recreate the table with the proper structure.