0

I cannot find the problem in my (Android) Java code.
The problem (according the logcat) is that there is no such column as reminder_date.
This is the error:

09-19 21:27:24.440  23689-23689/com.example.sanne.reminderovapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sanne.reminderovapplication, PID: 23689
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sanne.reminderovapplication/com.example.sanne.reminderovapplication.MainActivity}: android.database.sqlite.SQLiteException: no such column: reminder_date (code 1): , while compiling: SELECT reminder_id AS _id , reminder_title , reminder_description , reminder_date FROM reminder

I checked for the commas and spaces.
I hope there is a solution for this problem.

This is my code of the MySQLiteHelper:

public class MySQLiteHelper extends SQLiteOpenHelper{

// Database info
private static final String DATABASE_NAME = "reminderOVApp.db";
private static final int DATABASE_VERSION = 8;

// Assignments
public static final String TABLE_REMINDER = "reminder";
public static final String COLUMN_REMINDER_ID = "reminder_id";
public static final String COLUMN_REMINDER_TITLE = "reminder_title";
public static final String COLUMN_REMINDER_DESCRIPTION = "reminder_description";
public static final String COLUMN_REMINDER_DATE = "reminder_date";

// Creating the table
private static final String DATABASE_CREATE_REMINDERS =
        "CREATE TABLE " + TABLE_REMINDER +
                "(" +
                COLUMN_REMINDER_ID + " integer primary key autoincrement , " +
                COLUMN_REMINDER_TITLE + " text not null , " +
                COLUMN_REMINDER_DESCRIPTION + " text not null , " +
                COLUMN_REMINDER_DATE + " text not null " +
                ");";

// Mandatory constructor which passes the context, database name and database version and passes it to the parent
public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database)
{
    // Execute the sql to create the table assignments
    database.execSQL(DATABASE_CREATE_REMINDERS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    // When the database gets upgraded you should handle the update to make sure there is no data loss.
    // This is the default code you put in the upgrade method, to delete the table and call the oncreate again.
    if(oldVersion == 8)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REMINDER);
        onCreate(db);
    }
}

And this is the code of the DataSource Class:

public class DataSource {

private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] assignmentAllColumns = {MySQLiteHelper.COLUMN_REMINDER_ID, MySQLiteHelper.COLUMN_REMINDER_TITLE, MySQLiteHelper.COLUMN_REMINDER_DESCRIPTION, MySQLiteHelper.COLUMN_REMINDER_DATE};

public DataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
    database = dbHelper.getWritableDatabase();
    dbHelper.close();
}

// Opens the database to use it
public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

// Closes the database when you no longer need it
public void close() {
    dbHelper.close();
}

//Add a reminder to the database
public long createReminder(String reminder_title, String reminder_description, String reminder_date) {
    // If the database is not open yet, open it
    if (!database.isOpen()) {
        open();
    }

    ContentValues values = new ContentValues();

    values.put(MySQLiteHelper.COLUMN_REMINDER_TITLE, reminder_title);
    values.put(MySQLiteHelper.COLUMN_REMINDER_DESCRIPTION, reminder_description);
    values.put(MySQLiteHelper.COLUMN_REMINDER_DATE, reminder_date);
  //  values.put(MySQLiteHelper.COLUMN_REMINDER_TIME, reminder_time);

    long insertId = database.insert(MySQLiteHelper.TABLE_REMINDER, null, values);

    // If the database is open, close it
    if (database.isOpen()) {
        close();
    }

    return insertId;
}

//Change data of the specific reminder
public void updateReminder(Reminder reminder) {
    if (!database.isOpen()) {
        open();
    }

    ContentValues args = new ContentValues();
    args.put(MySQLiteHelper.COLUMN_REMINDER_TITLE, reminder.getTitle());
    args.put(MySQLiteHelper.COLUMN_REMINDER_DESCRIPTION, reminder.getDescription());
    args.put(MySQLiteHelper.COLUMN_REMINDER_DATE, reminder.getDate());

    database.update(MySQLiteHelper.TABLE_REMINDER, args, MySQLiteHelper.COLUMN_REMINDER_ID + "=?", new String[] { Long.toString(reminder.getId()) });

    if (database.isOpen()) {
        close();
    }
}


//Delete a reminder from the database
public void deleteReminder(long id) {
    if (!database.isOpen()) {
        open();
    }

    database.delete(MySQLiteHelper.TABLE_REMINDER, MySQLiteHelper.COLUMN_REMINDER_ID + " =?", new String[]{Long.toString(id)});

    if (database.isOpen()) {
        close();
    }
}

//Delete all reminders
public void deleteReminders() {
    if (!database.isOpen()) {
        open();
    }

    database.delete(MySQLiteHelper.TABLE_REMINDER, null, null);

    if (database.isOpen()) {
        close();
    }
}

//This way you can get the id and the reminder from the cursor.
private Reminder cursorToReminder(Cursor cursor) {
    try {

        Reminder reminder = new Reminder();

        reminder.setId(cursor.getLong(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_REMINDER_ID)));
        reminder.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_REMINDER_TITLE)));
        reminder.setDescription(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_REMINDER_DESCRIPTION)));
        reminder.setDate(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_REMINDER_DATE)));

        return reminder;

    }catch(CursorIndexOutOfBoundsException exception) {

        exception.printStackTrace();
        return null;

    }
}

//Get all the reminders to populate the listView --> ArrayList
public List<Reminder> getAllReminders() {
    if (!database.isOpen()) {
        open();
    }

    List<Reminder> reminders = new ArrayList<Reminder>();
    Cursor cursor = database.query(MySQLiteHelper.TABLE_REMINDER, assignmentAllColumns, null, null, null, null, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        Reminder assignment = cursorToReminder(cursor);
        reminders.add(assignment);
        cursor.moveToNext();
    }

    // make sure to close the cursor
    cursor.close();

    if (database.isOpen()) {
        close();
    }

    return reminders;
}

//A SimpleCursorAdapter requires a Cursor to get the data from the database instead of an ArrayList.
public Cursor getAllAssignmentsCursor()
{
    if (!database.isOpen()) {
        open();
    }

    Cursor cursor = database.rawQuery(
            "SELECT " +
                    MySQLiteHelper.COLUMN_REMINDER_ID + " AS _id , " +
                    MySQLiteHelper.COLUMN_REMINDER_TITLE + " , " +
                    MySQLiteHelper.COLUMN_REMINDER_DESCRIPTION + " , " +
                    MySQLiteHelper.COLUMN_REMINDER_DATE +
                    " FROM " + MySQLiteHelper.TABLE_REMINDER, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }

    if (database.isOpen()) {
        close();
    }
    return cursor;
}

//Get one reminder
public Reminder getReminder(long columnId) {

    if (!database.isOpen()) {
        open();
    }

    Cursor cursor = database.query(MySQLiteHelper.TABLE_REMINDER, assignmentAllColumns, MySQLiteHelper.COLUMN_REMINDER_ID + "=?", new String[] { Long.toString(columnId)}, null, null, null);
    cursor.moveToFirst();

    Reminder assignment = cursorToReminder(cursor);

    cursor.close();

    if (database.isOpen()) {
        close();
    }

    return assignment;
}}

And my MainActivity Class:

public class AddActivity extends AppCompatActivity {

private DataSource datasource;
private EditText addReminderEditText;
private EditText descriptionEditText;
private EditText dateEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

    datasource = new DataSource(this);
    addReminderEditText = (EditText) findViewById(R.id.add_reminder_editText);
    descriptionEditText = (EditText) findViewById(R.id.description_reminder_editText);
    dateEditText = (EditText) findViewById(R.id.date_reminder_editText);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_add, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.add_assignment_menu_save) {

        //Sending the data back to MainActivity
        long reminderId = datasource.createReminder(addReminderEditText.getText().toString(), descriptionEditText.getText().toString(), dateEditText.getText().toString());

        Intent resultIntent = new Intent();
        resultIntent.putExtra(MainActivity.EXTRA_REMINDER_ID, reminderId);
        setResult(Activity.RESULT_OK, resultIntent);

        finish();

        return true;
    }

    return super.onOptionsItemSelected(item);
}}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
secretos
  • 11
  • 6

1 Answers1

0

Well, it seems like reminder_date has been added after the first code execution.
Simply uninstall and reinstall your app.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115