I know the question has been asked many times, but I just can not find my mistake. I think I did everything right when creating the table.
The error Message is:
android.database.sqlite.SQLiteException: table GPSLocation has no column named Number (code 1):
I checked blanks and punctuation several times. Please help me to find the mistake.
Here is my Database Class:
private GPSDatabase(Context context) {
super(context, DB_NAME, null, VERSION);
}
public static GPSDatabase getInstance (final Context context){
if (INSTANCE == null){
INSTANCE = new GPSDatabase(context);
}
return INSTANCE;
}
Here I create the table
@Override
public void onCreate(final SQLiteDatabase db) {
String createQuery =
"CREATE TABLE " + TABLE_NAME + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + LATITUDE_COLUMN + " REAL DEFAULT NULL, " + LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
db.execSQL(createQuery);
Log.d(TAG, "Table created");
}
The onUpgrade() Method destroys the existing table
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(dropTable);
Log.d(TAG,"Table deleted");
onCreate(db);
}
Create a new database entry
public GPSLocation createGPSLocation (final GPSLocation loc){
long newID = -1;
try{
Log.d(TAG,"createGPSLocation begin");
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NUMBER_COLUMN, loc.getNumber());
values.put(DISTANCE_COLUMN, loc.getDistance());
values.put(LATITUDE_COLUMN, loc.getLatitude());
values.put(LONGITUDE_COLUMN, loc.getLongitude());
newID = db.insert(TABLE_NAME, null, values);
db.close();
}catch (SQLiteException e){
Log.e(TAG,"insert");
}finally {
Log.d(TAG,"insert with ID " + newID);
}
Log.d(TAG, "createGPSLocation successful");
return readGPSLocation(newID);
}
public GPSLocation readGPSLocation(final long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{ID_COLUMN, NUMBER_COLUMN, DISTANCE_COLUMN,
LATITUDE_COLUMN, LONGITUDE_COLUMN}, ID_COLUMN + " = ?", new String[]{String.valueOf(id)}, null, null, null);
GPSLocation loc = null;
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
loc = new GPSLocation(cursor.getInt(cursor.getColumnIndex(NUMBER_COLUMN)));
loc.setId(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)));
loc.setDistance(cursor.getInt(cursor.getColumnIndex(DISTANCE_COLUMN)));
loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
}
db.close();
Log.d(TAG, "read");
return loc;
}
}