1

I've one problem when I try to compile my android application.

If I add a column (FECHA) to a database already, an error appears:

Android.database.sqlite.SQLiteException: no such column: fecha (code 1): , while compiling: SELECT _id, task, place, importance, fecha FROM todolist ORDER BY importance

This is the class of the database

public class DataBaseHelper {
    private Context mCtx = null;
    private DataBaseHelperInternal mDbHelper = null;
    private SQLiteDatabase mDb = null;
    private static final String DATABASE_NAME = "TODOLIST";
    private static final int DATABASE_VERSION = 200;
    // tabla y campos
    private static final String DATABASE_TABLE_TODOLIST = "todolist";
    public static final String SL_ID = "_id";
    public static final String SL_ITEM = "task";
    public static final String SL_PLACE = "place";
    public static final String SL_DATE = "fecha";
    public static final String SL_IMPORTANCE = "importance";
    public static final String SL_DESCRIPTION = "description";

    // SQL de creación de la tabla
    private static final String DATABASE_CREATE_TODOLIST =
            "create table " + DATABASE_TABLE_TODOLIST
                    +" (" + SL_ID + " integer primary key,"
                    + SL_ITEM + " text not null,"
                    + SL_PLACE + " text not null,"
                    + SL_IMPORTANCE + " integer not null,"
                    + SL_DESCRIPTION + " text,"
                    + SL_DATE +  " text"
                    + ");";

    //constructor
    public DataBaseHelper(Context ctx) {
        this.mCtx = ctx;
    }
    //clase privada para control de la SQLite
    private static class DataBaseHelperInternal extends SQLiteOpenHelper {
        public DataBaseHelperInternal(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            createTables(db);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            deleteTables(db);
            createTables(db);
        }
        private void createTables(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE_TODOLIST);
        }

        private void deleteTables(SQLiteDatabase db) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_TODOLIST);
        }
    }

    public DataBaseHelper open() throws SQLException {
        mDbHelper = new DataBaseHelperInternal(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    //obtener todos los elementos
    public Cursor getItems() {
        return mDb.query(DATABASE_TABLE_TODOLIST, new String[] {SL_ID,SL_ITEM,SL_PLACE,SL_IMPORTANCE,SL_DATE}, null, null, null, null, SL_IMPORTANCE);
    }

    //crear elemento
    public long insertItem(String item, String date, String place, String description, int importance){
        ContentValues initialValues = new ContentValues();
        initialValues.put(SL_IMPORTANCE, importance);
        initialValues.put(SL_ITEM, item);
        initialValues.put(SL_DATE, date);
        initialValues.put(SL_PLACE, place);
        initialValues.put(SL_DESCRIPTION, description);
        return mDb.insert(DATABASE_TABLE_TODOLIST, null, initialValues);
    }

    public Cursor getItem(int itemId){
        return mDb.rawQuery(" select "+ SL_ITEM+","+ SL_DATE+","+ SL_PLACE+","+ SL_DESCRIPTION+","+SL_IMPORTANCE + "," + SL_ID + " from " + DATABASE_TABLE_TODOLIST  + " where " + SL_ID + "= ?",new String[]{Integer.toString(itemId)});
    }

The error occurs when you try to collect data from the table

Thanks for everyone!!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Increment DATABASE_VERSION so the database schema is updated. You may have added the column in your code after the table was created.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

I was like this before

private static final int DATABASE_VERSION = 200;

So I increase the dataversion like that?

 private static final int DATABASE_VERSION = 201;