I have code that downloads a set of data (item and value) paired. The value can change over time so I have an update function in my code. The problem is, mDb.update ALWAYS throws an error, even after the first time the code is run successfully and the table is created(I know this because the values show up later in the app) also names[c] is “Android”:
android.database.sqlite.SQLiteException: no such column: Android: , while compiling: UPDATE ThemeFavorites SET Favorites=? WHERE Name=Android
What baffles me is that it appears the code is looking for a column named Android, rather than the KEY_NAME column…can anyone shed some light?
public void updateFavoritesValue(String[] names, String[] values) {
int c = 0;
while (c< names.length) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_FAVORITES_VALUE, values[c]);
try {
mDb.update(TABLE_THEME_FAVORITES, initialValues, KEY_NAME + "=" + names[c], null);
// TODO this always flags exception :(
} catch (Exception e) { // Table isn't populated yet, must insert new row
initialValues.put(KEY_NAME, names[c]);
initialValues.put(KEY_USER_FAVORITE, "No");
mDb.insert(TABLE_THEME_FAVORITES, null, initialValues);
}
c++;
}
}
My table creation code just in case
public static final String KEY_NAME = "Name";
private static final String TABLE_THEME_FAVORITES = "ThemeFavorites";
public static final String KEY_FAVORITES_VALUE = "Favorites";
public static final String KEY_USER_FAVORITE = "UserFavorite";
private static final String DATABASE_CREATE_THEME_FAVORITES = "create table " + TABLE_THEME_FAVORITES + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, "
+ KEY_USER_FAVORITE + " text not null, "
+ KEY_FAVORITES_VALUE + " text not null);";
You need to delimit strings with quotes:
mDb.update(TABLE_THEME_FAVORITES, initialValues, KEY_NAME + "='" + names[c] + "'", null);If a string is not in quotes it is assumed to be a field name in a table.