I have looked at some of the other questions on this but I still can't seem to find my problem.
I am getting the following error:
Caused by: android.database.sqlite.SQLiteException: no such column: category (code 1): , while compiling: SELECT _id, category, vehicle, distance, date, note FROM Trip
I am new to android but I can't see any reason for this error. The column is clearly created
public class TripsDbAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_VEHICLE = "vehicle";
public static final String KEY_DISTANCE = "distance";
public static final String KEY_DATE = "date";
public static final String KEY_NOTE = "note";
private static final String TAG = "TripsDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "CarbonFootprint";
private static final String SQLITE_TABLE = "Trip";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_CATEGORY + "," +
KEY_VEHICLE + "," +
KEY_DISTANCE + "," +
KEY_DATE + "," +
KEY_NOTE + ")";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public TripsDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public TripsDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
The problem seems to be cause by calling this method:
public Cursor fetchAllTrips() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CATEGORY, KEY_VEHICLE, KEY_DISTANCE, KEY_DATE, KEY_NOTE},
null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The weird thing is that I was running the program without errors yesterday and I didn't make any changes today. I've spent several hours trying to figure it out but I'm at a loss on this.
Aucun commentaire:
Enregistrer un commentaire