samedi 27 juin 2015

Query to select rows in sqlite database which are between today's date and 7 days time

I have created a small application to track university assignments. Using sqlite database the user can store information such as the assignment name, when it is due and notes on the assignment. I have created a listview which will show upcoming assignments, so any assignments which is due in the next 7 days. I cant seem to get the query right, anyone that could help?? Thanks

//My DbHelper class
public class DbHelper extends SQLiteOpenHelper {

public static final String ASSIGNMENT_ID  = "_id";
public static final String ASSIGNMENT_VALUE = "assignment_value";
public static final String SUBJECT = "subject";
public static final String ASSIGNMENT_TITLE = "assignment_title";
public static final String DUE_DATE = "due_date";
public static final String NOTES_ASSIGNMENT = "notes";
public static final String TABLE_NAME_ASSIGNMENT = "assignment_information";

private static final String DATABASE_NAME = "assignment_info.db";

private static final String ADD_QUERY = "CREATE TABLE "+
        TABLE_NAME_ASSIGNMENT+"("+
        ASSIGNMENT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
        SUBJECT+" TEXT,"+
        ASSIGNMENT_VALUE+" INTEGER,"+
        ASSIGNMENT_TITLE+" TEXT,"+
        DUE_DATE+" DATETIME,"+
        NOTES_ASSIGNMENT+" TEXT);";

    //My query (so far) to return a cursor with the selected assignments
    public Cursor getUpcomingAssignment(SQLiteDatabase sqLiteDatabase){

    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(gc.getTime());

    String sql = "SELECT * FROM " + TABLE_NAME_ASSIGNMENT + " WHERE "+ DUE_DATE + " >= date('now', '+7 days')";

    Cursor mycursor = sqLiteDatabase.rawQuery(sql, null);
    return mycursor;
}

Aucun commentaire:

Enregistrer un commentaire