Search code examples
androidsqlitesqliteopenhelper

android.database.sqlite.SQLiteException: no such column: while compiling: UPDATE


I am trying to update some columns in a row in SQLite in an Android app, however I am stuck at this error which I just don't know where it comes from.

android.database.sqlite.SQLiteException: no such column: location (code 1): , while compiling: UPDATE user SET email=?,location=?,mobile=? WHERE id=?

This is the code I use to update info:

public void updateUserInfo(String mobile, String email, String location) {
    SQLiteDatabase db = this.getWritableDatabase();

    String[] tablei = new String[1];
    tablei[0] = "1";

    ContentValues values = new ContentValues();
    values.put(KEY_MOBILE, mobile); // Mobile
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_LOCATION, location); // Location

    // Inserting Row
    //long id = db.update().insert(TABLE_USER, null, values);
    long id = db.update(TABLE_USER, values, "id=?", tablei);
    db.close(); // Closing database connection

    Log.d(TAG, "Info of user updated in sqlite: " + id);
}

This is my table in which there are all the columns used:

   private static final String KEY_ID = "id";
   private static final String KEY_LOCATION = "location"; 
                          .....

    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE," + KEY_APIKEY + " TEXT,"
            + KEY_MOBILE + " TEXT," + KEY_LOCATION + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ")";

Thank you in advance for your help !


Solution

  • I think you modified the database scheme after the first install of the app.

    You have to write an update script from the old version of the Database to the new version. (ALTER TABLE tablename ADD COLUMN ....)

    Or delete and install the app again.