dimanche 30 août 2015

Update function not working correctly [duplicate]

This question already has an answer here:

I made a class DBHelper.java for implementing SQLiteDatabase. But it is not working and giving the error:

android.database.sqlite.SQLiteException: no such column: Reading1 (code 1): , while compiling: UPDATE sensors SET Reading3=?,Status=?,Reading2=?,Reading1=?,Reading4=? WHERE Name = ?

I can't seem to find the way around this.

My code is:

public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Readings.db";
public static final String SENSORS_TABLE_NAME = "sensors";
public static final String SENSORS_COLUMN_ID = "Id";
public static final String SENSORS_COLUMN_NAME = "Name";
public static final String SENSORS_COLUMN_1 = "Reading1";
public static final String SENSORS_COLUMN_2 = "Reading2";
public static final String SENSORS_COLUMN_3 = "Reading3";
public static final String SENSORS_COLUMN_4 = "Reading4";
public static final String SENSORS_COLUMN_STATUS = "Status";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + SENSORS_TABLE_NAME + "(" + SENSORS_COLUMN_ID + " INTEGER PRIMARY KEY," + SENSORS_COLUMN_NAME + " TEXT" +
                    SENSORS_COLUMN_1 + " REAL," + SENSORS_COLUMN_2 + " REAL," +
                    SENSORS_COLUMN_3 + " REAL," + SENSORS_COLUMN_4 + " REAL," + SENSORS_COLUMN_STATUS + " INTEGER)"
    );
    db.setLocale(Locale.getDefault());
    db.setVersion(1);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + SENSORS_TABLE_NAME);
    onCreate(db);
}

public boolean insertSensor(String name, boolean status) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("Name", name);
    contentValues.put("Reading1", 0);
    contentValues.put("Reading2", 0);
    contentValues.put("Reading3", 0);
    contentValues.put("Reading4", 0);
    contentValues.put("Status", status);
    db.insert("sensors", null, contentValues);
    return true;
}

public Cursor getData(String Name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from sensors where Name='" + Name + "'", null);
    return res;
}

public boolean updateSensor(String Name, float Reading1, float Reading2, float Reading3, float Reading4, boolean Status) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(SENSORS_COLUMN_1, Reading1);
    contentValues.put(SENSORS_COLUMN_2, Reading2);
    contentValues.put(SENSORS_COLUMN_3, Reading3);
    contentValues.put(SENSORS_COLUMN_4, Reading4);
    contentValues.put(SENSORS_COLUMN_STATUS, Status);
    db.update("sensors", contentValues, "Name = ?", new String[]{Name});
    return true;
}

}

Aucun commentaire:

Enregistrer un commentaire