没有这样的列:task_date(代码1):

时间:2016-01-14 17:43:31

标签: android sqlite android-sqlite

没有这样的列:task_date(代码1):,编译时:SELECT * FROM tasks_table WHERE task_status =? OR task_status =? ORDER BY task_date 多次检查代码,找不到错误!!

DBHelper

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "reminder_database";

    public static final String TASKS_TABLE = "tasks_table";

    public static final String TASK_TITLE_COLUMN = "task_title";
    public static final String TASK_DATE_COLUMN = "task_date";
    public static final String TASK_PRIORITY_COLUMN = "task_priority";
    public static final String TASK_STATUS_COLUMN = "task_status";
    public static final String TASK_TIME_STAMP_COLUMN = "task_time_stamp";

    private static final String TASKS_TABLE_CREATE_SCRIPT =  "CREATE TABLE "
            + TASKS_TABLE + " (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + TASK_TITLE_COLUMN + " TEXT NOT NULL, "
            + TASK_DATE_COLUMN + " LONG, " + TASK_PRIORITY_COLUMN + " INTEGER, "
            + TASK_STATUS_COLUMN + " INTEGER, " + TASK_TIME_STAMP_COLUMN + " LONG);";


    public static final String SELECTION_STATUS = DBHelper.TASK_STATUS_COLUMN + " = ?";
    public static final String SELECTION_TIME_STAMP = TASK_TIME_STAMP_COLUMN + " = ?";

    private DBQueryManager dbQueryManager;
    private DBUpdateManager dbUpdateManager;



    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        dbQueryManager = new DBQueryManager(getReadableDatabase());
        dbUpdateManager = new DBUpdateManager(getWritableDatabase());
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TASKS_TABLE_CREATE_SCRIPT);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE " + TASKS_TABLE);
         onCreate(db);
    }

    public void saveTask(ModelTask task){

        ContentValues newValues = new ContentValues();

        newValues.put(TASK_TITLE_COLUMN,task.getTitle());
        newValues.put(TASK_DATE_COLUMN,task.getDate());
        newValues.put(TASK_STATUS_COLUMN,task.getStatus());
        newValues.put(TASK_PRIORITY_COLUMN,task.getPriority());
        newValues.put(TASK_TIME_STAMP_COLUMN, task.getTimeStamp());

        getWritableDatabase().insert(TASKS_TABLE, null, newValues);

    }

    public DBUpdateManager update() {
        return dbUpdateManager;
    }

    public DBQueryManager query() {
        return dbQueryManager;
    }

    public void removeTask(long timeStamp){
        getWritableDatabase().delete(TASKS_TABLE,SELECTION_TIME_STAMP,new String[]{Long.toString(timeStamp)});

    }
}

DBQueryManager

public class DBQueryManager {
    private SQLiteDatabase sqLiteDatabase;

    public DBQueryManager(SQLiteDatabase sqLiteDatabase) {
        this.sqLiteDatabase = sqLiteDatabase;
    }


    public ModelTask getTask(long timeStamp){
        ModelTask modelTask = null;
        Cursor c = sqLiteDatabase.query(DBHelper.TASKS_TABLE,null,DBHelper.SELECTION_TIME_STAMP,
                new String[]{Long.toString(timeStamp)},null,null,null);

        if (c.moveToFirst()){

            String title = c.getString(c.getColumnIndex(DBHelper.TASK_TITLE_COLUMN));
            long date =  c.getLong(c.getColumnIndex(DBHelper.TASK_DATE_COLUMN));
            int priority = c.getInt(c.getColumnIndex(DBHelper.TASK_PRIORITY_COLUMN));
            int status = c.getInt(c.getColumnIndex(DBHelper.TASK_STATUS_COLUMN));
            modelTask = new ModelTask(title,date,priority,status,timeStamp);

        }
        c.close();
        return modelTask;

    }


    public List<ModelTask> getTask(String selection , String[] selectionArgs,String orderBy){
        List<ModelTask> tasks = new ArrayList<>();

        Cursor c = sqLiteDatabase.query(DBHelper.TASKS_TABLE, null,selection,selectionArgs,null,null,orderBy);

        if (c.moveToFirst()){
            do {

                String title = c.getString(c.getColumnIndex(DBHelper.TASK_TITLE_COLUMN));
                long date =  c.getLong(c.getColumnIndex(DBHelper.TASK_DATE_COLUMN));
                int priority = c.getInt(c.getColumnIndex(DBHelper.TASK_PRIORITY_COLUMN));
                int status = c.getInt(c.getColumnIndex(DBHelper.TASK_STATUS_COLUMN));
                long timeStamp = c.getInt(c.getColumnIndex(DBHelper.TASK_TIME_STAMP_COLUMN));

                ModelTask modelTask = new ModelTask(title,date,priority,status,timeStamp);
                tasks.add(modelTask);



            }while (c.moveToNext());
        }
        c.close();
        return tasks;


    }
}

1 个答案:

答案 0 :(得分:1)

  

我们打赌你在上一次运行后添加了task_date吗?

正如Hrundi V. Bakshi所说,如果在运行程序一次后将该列添加到表中,则需要增加数据库版本,以便调用onUpgrade()方法,这会将列添加到您的表

每当对数据库进行结构更改时,都应该增加版本,以便为SQLite DB启动正确的回调,以使用新的字段/列重新创建表。

public static final int DATABASE_VERSION = 1; // Your database version, increment it.

public static final int DATABASE_VERSION = 2;