首页 > 解决方案 > 更新行时 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 逐行更新温度数据

标签: javaandroidsqliteandroid-sqlite

解决方案


我认为您已经更改了列名或添加了新的列名(城市)。所以你可以通过两种方式修复它

  1. 通过从手机卸载应用程序
  2. 在升级方法中添加列名。

例子:

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

推荐阅读