错误SQLite在编译时没有这样的列(代码1)

时间:2016-08-15 19:50:27

标签: android sqlite

当我尝试编译我的android apliccation时,我遇到了一个问题。

如果我向数据库添加一列(FECHA)已经出现错误:

android.database.sqlite.SQLiteException:没有这样的列:fecha(代码1):,同时编译:SELECT _id,task,place,importance,fecha FROM todolist ORDER BY important

这是de数据库的类

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

当您尝试从表中收集数据时发生错误

谢谢大家!!

1 个答案:

答案 0 :(得分:0)

之前我是这样的

private static final int DATABASE_VERSION = 200;

所以我这样增加了数据转换?

 private static final int DATABASE_VERSION = 201;