java - 更新行时 SQLite 中没有这样的列错误
问题描述
我正在尝试更新数据库中的行中的数据,但我发现没有这样的列(没有这样的列“莫斯科”或其他列)的错误
这是 DBHelper 代码:
public static final String tableName = "currentWeather";
public static final String KEY_ID = "_id";
public static final String cityName = "city";
public static final String cityTemp = "temperature";
并创建数据库:
sqLiteDatabase.execSQL("create table " + tableName + "(" + KEY_ID + "
integer primary key autoincrement,"
+ cityName + " text," + cityTemp + " text, " + " UNIQUE(" + cityName +
"))");
当我尝试执行 execSQl 作为响应时显示错误:
sqLiteDatabase.execSQL(
"UPDATE " + DBHelper.tableName + " SET " +
DBHelper.cityTemp + "=" +
response.body().getForecastMain().getTemp() + "
WHERE "
+ DBHelper.cityName + "=" + cityName);
我希望按 cityName 逐行更新温度数据
解决方案
我认为您已经更改了列名或添加了新的列名(城市)。所以你可以通过两种方式修复它
- 通过从手机卸载应用程序
- 在升级方法中添加列名。
例子:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// If you need to add a column
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
}
}
推荐阅读
- reactjs - 不支持扩展 create-react-app 获取 src/ 之外的相对导入
- python - 如何将 Stanza Word 标记化数据保存在单个 CSV 文件中?
- javascript - 如何在 javascript 中存储玩家猜测并检查字母字符
- ubuntu - 如何连接到使用 vmwares vmrun CLI 命令启动的虚拟机的 GUI?
- c# - 如何为不和谐机器人编写单元测试
- spring-boot - 使用 String 时 MockMVC @PathVariable 不起作用
- oracle - Apex 链接构建器值 - 项目未显示在目标页面上
- r - 使用频率表数据中的 ggplot 分组条形图
- python - 使用 Pandas 比较每 2 行并显示不同的
- amazon-web-services - 如何在 Windows Server 上的 Elastic Beanstalk 部署中正确使用 AWS Secrets Manager