SQL 更新始终抛出错误
我有下载一组配对数据(项目和值)的代码。该值可能会随着时间的推移而变化,因此我的代码中有一个更新函数。问题是,mDb.update 总是抛出错误,即使在第一次成功运行代码并创建表之后(我知道这一点,因为值稍后显示在应用程序中)也名称[c] 是“Android” :
android.database.sqlite.SQLiteException:没有这样的列:Android:,编译时:UPDATE ThemeFavorites SET favorites =? WHERE 名称=Android
令我困惑的是,代码似乎正在寻找名为 Android 的列,而不是 KEY_NAME 列...任何人都可以透露一些信息吗?
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++;
}
}
我的表创建代码以防万一
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);";
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);";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要用引号分隔字符串:
mDb.update(TABLE_THEME_FAVORITES, initialValues, KEY_NAME + "='" + names[c] + "'", 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.
难道不应该是
UPDATE ThemeFavorites SET favorites= 吗? WHERE Name='Android'
通常需要在 SQL 查询中分隔字符串
Shouldn't that be
UPDATE ThemeFavorites SET Favorites=? WHERE Name='Android'
You usually need to delimit strings in SQL queries