E/SQLiteLog: (1) table Estudiantes has no column named calificacion in android

Viewed 843

This is the class that I´m using

public class DBHelper extends SQLiteOpenHelper {

    private static final String DB_FILE = "Database.db";
    private static final String TABLE = "Estudiantes";
    private static final String FIELD_ID = "id";
    private static final String FIELD_NAME = "nombre";
    private static final String FIELD_GRADE = "calificacion";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE + "(" +
                FIELD_ID + " INTEGER PRIMARY KEY, " +
                FIELD_NAME + " TEXT, " +
                FIELD_GRADE + " INTEGER);";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS ?";
        String[] params = {TABLE};
        db.execSQL(query, params);
    }

    public void save(String nombre, int calificacion){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues valores = new ContentValues();

        valores.put(FIELD_NAME, nombre);
        valores.put(FIELD_GRADE, calificacion);

        db.insert(TABLE, null, valores);
    }

    public int delete(String nombre){
        SQLiteDatabase db = getWritableDatabase();
        String clause = FIELD_NAME + " = ?";
        String[] args = {nombre};
        return db.delete(TABLE, clause, args);
    }

    public int find(String nombre){
        SQLiteDatabase db = getReadableDatabase();
        String filtrito = FIELD_NAME + " = ?";
        String[] args = {nombre};

        Cursor c = db.query(TABLE, null, filtrito, args, null, null, null);
        int result = -1;

        if(c.moveToFirst()) {
            result = c.getInt(2);
        }

        return result;
    }
}

This is the error message I get:

E/SQLiteLog: (1) table Estudiantes has no column named calificacion
E/SQLiteDatabase: Error inserting calificacion=80 nombre=Fer
android.database.sqlite.SQLiteException: 
table Estudiantes has no column named calificacion (code 1): , while compiling: 
INSERT INTO Estudiantes(calificacion,nombre) VALUES (?,?)
1 Answers

It looks like that you added calificacion column later in the database. I would do one of the following:

  1. Uninstalling and re-installing your app.
  2. The best and better approach is to drop and recreate Estudiantes table in onUpdate method, and increase the db version wheneveryou change the schema.
Related