-1

This is the error I get

Caused by: java.lang.IllegalArgumentException: column 'date' does not exist

Here is my code

import java.util.ArrayList;


class DBHelper extends SQLiteOpenHelper {

    DBHelper(Context context) {
        super(context, DATABASE_NAME , null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "create table " + RESULTS_TABLE_NAME +
                        " (" + RESULTS_COLUMN_ID + " integer primary key," + RESULTS_COLUMN_DATE + " text," + RESULTS_COLUMN_DURATION + " text," + RESULTS_COLUMN_DISTANCE + " text," + RESULTS_COLUMN_PACE + " text, " + RESULTS_COLUMN_CALORIES + " text)"
        );

    }

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

    boolean insertLocation(String date, String duration, String distance, String pace, String calories) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(RESULTS_COLUMN_DATE, date);
        contentValues.put(RESULTS_COLUMN_DURATION, duration);
        contentValues.put(RESULTS_COLUMN_DISTANCE, distance);
        contentValues.put(RESULTS_COLUMN_PACE, pace);
        contentValues.put(RESULTS_COLUMN_CALORIES, calories);
        db.insert(RESULTS_TABLE_NAME, null, contentValues);
        return true;
    }

    ArrayList<MyLayoutObject> getAllResults() {
        ArrayList<MyLayoutObject> array_list = new ArrayList<>();

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor res =  db.rawQuery( "select * from " + RESULTS_TABLE_NAME, null );
//        String[] columns = new String[]{ RESULTS_COLUMN_DATE, RESULTS_COLUMN_DISTANCE, RESULTS_COLUMN_DURATION, RESULTS_COLUMN_PACE, RESULTS_COLUMN_CALORIES};
//        Cursor res = db.query(DATABASE_NAME, columns, null, null, null, null, null);
        res.moveToFirst();

        while(!res.isAfterLast()){
            array_list.add(new MyLayoutObject(
                    res.getString(res.getColumnIndexOrThrow(RESULTS_COLUMN_DATE )), // This is throwing the error I mentioned
                    res.getString(res.getColumnIndexOrThrow(RESULTS_COLUMN_DURATION)),
                    res.getString(res.getColumnIndexOrThrow(RESULTS_COLUMN_DISTANCE)),
                    res.getString(res.getColumnIndexOrThrow(RESULTS_COLUMN_PACE)),
                    res.getString(res.getColumnIndexOrThrow(RESULTS_COLUMN_CALORIES))));
            res.moveToNext();
        }
        res.close();
        return array_list;
    }

}

//MyLayoutObject is just 5 strings in trenchcoat, acting like a real object.

Just kidding, it's 5 strings with methods to get them, nothing else.

It's for an app that records my results when running, kind of like Endomondo. This database should hold the recorded performances, even after I turn the app off. I use the method getAllResults() to build a ListedView, but it crashes because of the getAllResults() method. Toasts and System.out.println() don't print anything.

edit:

    private static final String RESULTS_COLUMN_DATE = "date";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You can do like this, 1. Change the Database name/increase the version of database 2. uninstall the app and reinstall again.

Vijay
  • 156
  • 1
  • 14
0

seems you have an existing db with a different Db structure try ununstalling your app or change your db version

Donny Dominic
  • 144
  • 1
  • 15