SQLite更新行错误没有这样的这样的列

时间:2015-04-15 20:52:16

标签: android sqlite

SQLite数据库不断出现no such column错误。我尝试了rawQueryexecQuery,但我得到了相同的结果。我认为这是一个语法错误,但我不知道如何解决它。

这是我的代码:

public class DatabaseHelper extends SQLiteOpenHelper {

  private static final String DATABASE_NAME = "Details_z";

  // private SQLiteDatabase database;

  public DatabaseHelper(Context context)
  {
    super(context, DATABASE_NAME, null, 1);
    // Note - 1 is a version number
    // for the database
    // get a reference to our database which we'll use later to insert

  }

  @Override
  // Automatically called if the database needs creating
  public void onCreate(SQLiteDatabase db)
  {

    db.execSQL("CREATE TABLE IF NOT EXISTS Station(Stationid VARCHAR,Stationname VARCHAR,Stationtype VARCHAR,"
      + "facility VARCHAR,Stationlocal VARCHAR, wifi VARCHAR, toilet VARCHAR, lift VARCHAR, disable VARCHAR, date VARCHAR, rating VARCHAR, comments VARCHAR);");

  }

  @Override
  // Automatically called if database version number changes
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  {

    // bit mean to lose old data but at least we'll warn this is happening
    android.util.Log.w(this.getClass().getName(), DATABASE_NAME
      + " database upgrade to version " + newVersion
      + " old data lost");
    //db.execSQL("DROP TABLE IF EXISTS details");
    onCreate(db);
  }

  // Called by Persist1 to insert data entered by the user into 3
  // the database. We could use raw SQL (execSQL) but it is better to
  // use the purpose built insert method.
  public void insertDetails(String Stationid, String Stationname,
      String stationtype, String wifi, String toilet, String lift,
      String disable, String Stationlocal)
  {
    SQLiteDatabase data = this.getWritableDatabase();
    ContentValues rowValues = new ContentValues();
    rowValues.put("stationid", Stationid);
    rowValues.put("stationname", Stationname);
    rowValues.put("stationtype", stationtype);
    rowValues.put("wifi", wifi);
    rowValues.put("toilet", toilet);
    rowValues.put("lift", lift);
    rowValues.put("disable", disable);
    rowValues.put("Stationlocal", Stationlocal);
    data.insert("Station", null, rowValues);
    data.close();
  }

  public String searchstation(String ename)
  {

    SQLiteDatabase data = this.getWritableDatabase();

    Cursor q = data.rawQuery(
        "Select Stationname FROM Station WHERE Stationname  " + "='"
            + ename + "' limit 1", null);
    q.moveToFirst();
    return q.getString(q.getColumnIndex("Stationname"));

  }

  public void addRating(String name, String ndate, String nrating,
      String ncomment)
  {

    SQLiteDatabase data = this.getWritableDatabase();
    data.execSQL("update Station Set " + " date = " + ndate
        + "," + " rating = " + nrating + "," + " comments = "
        + ncomment + " WHERE Stationname = '" + name + "'");


        data.close();
  }
}

1 个答案:

答案 0 :(得分:1)

编辑:最好只使用update()方法:

public void addRating(String name, String nDate, String nRating, String nComment)
{
   SQLiteDatabase data = this.getWritableDatabase();

   ContentValues newValues = new ContentValues();
   newValues.put("date", nDate);
   newValues.put("rating", nRating);
   newValues.put("comments", nComment);

   String[] args = new String[]{name};
   data.update("Station", newValues, "Stationname =?", args);

   data.close(); //close the database

}

添加此导入:

import android.content.ContentValues;

请参阅Documentation Here