android.database.sqlite.SQLiteException:没有这样的列:编译时:UPDATE

时间:2016-09-15 09:57:51

标签: android sqlite sqliteopenhelper

我正在尝试在Android应用中更新SQLite中的某些列,但是我遇到了这个错误,我不知道它来自哪里。

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

这是我用来更新信息的代码:

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);
}

这是我的表格,其中包含所有列:

   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" + ")";

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

我认为您在首次安装应用程序后修改了数据库方案。

您必须将旧版数据库的更新脚本编写到新版本。 (ALTER TABLE tablename ADD COLUMN ....

或者再次删除并安装该应用程序。