SQLiteOpenHelper给出了IllegalArgumentException - 列不存在。 Android的

时间:2016-11-19 12:59:06

标签: android sqlite android-sqlite

这是我得到的错误

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

这是我的代码

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.

开玩笑,它的5个字符串有方法来获取它们,没有别的。

适用于在运行时记录我的搜索结果的应用,有点像Endomondo。即使在关闭应用程序之后,此数据库也应保留录制的演奏。我使用方法getAllResults()来构建ListedView,但由于getAllResults()方法而崩溃。 Toast和System.out.println()不打印任何内容。

编辑:

    private static final String RESULTS_COLUMN_DATE = "date";

2 个答案:

答案 0 :(得分:0)

你可以这样做, 1.更改数据库名称/增加数据库版本 2.卸载应用程序并重新安装。

答案 1 :(得分:0)

您似乎有一个具有不同Db结构的现有数据库尝试取消安装您的应用程序或更改您的数据库版本