我的SQLite Update语句一直在第一个字段中插入0,并在

时间:2016-03-10 14:11:25

标签: android database sqlite android-sqlite

我面临一个非常烦人的错误,就是它不断向第一列输入0,然后输入其余的值。

这是我的代码:

System.out.println(forename);
String statement = "UPDATE " + USER_TABLE + " SET " + FORENAME + " = " + "'" + forename + "'"
+ " AND " + SURNAME + " = " + "'" + surname + "'" + " AND " + EMAIL + " = " + "'" + email + "'"  
+ "WHERE " + USER_ID + " = " + userID;
sqLiteDatabase.execSQL(statement);

因此,如果forename为Luke,它将打印Luke,但在运行此查询后,表格将如下所示:

UserID  |  Forename   |  Surname   | Email
____________________________________________

1            0           Smith        luke@s.com

正如您所看到的那样Luke未放入Forename字段,尽管我可以清楚地看到Luke

中打印的值LogCat

1 个答案:

答案 0 :(得分:1)

应该使用ContentValues执行此任务:

例如:

    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(FORENAME, forename);
    values.put(SURNAME, surname);
    values.put(EMAIL, email);
    database.update(USER_TABLE, values, USER_ID + "=?", new String[] {userID});
    database.close();
    return true;