0

hello guys for the last three days I have been working on a simple query , but it is just not working , I'm trying to prevent register departure without having registered arrival , here is the code in my DB helper,not sure what is wrong with that query!!

public Boolean HasShowedUpToday(int check_id)
{  
    Boolean attend;
    SQLiteDatabase db=this.getWritableDatabase();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
    time=Calendar.getInstance().getTime();
    String dateoftoday= dateFormat.format(time);       
    Cursor c = db.rawQuery("SELECT * FROM attendance where user_id = '"+check_id+"' and arrival_time ='"+dateoftoday+"' and departure_time ='';", null);
    if (c.moveToFirst())
    {

        attend=true;
    }
    else
    {
        attend=false;
    }
    return attend;
}
user7439667
  • 144
  • 8
  • locale when operating on database? Locale is important when communicating with user not with database! – Marek R Aug 25 '17 at 09:25
  • @ Marek R so should i change the format that i have saved it with in db ? – Zahraa Fatma Aug 25 '17 at 09:35
  • Why there are extra ' in your query? Don't you think it should be like this -> `Cursor c = db.rawQuery("SELECT * FROM attendance where user_id = "+check_id+" and arrival_time ="+dateoftoday+" and departure_time =;", null);` – Lalit Fauzdar Aug 25 '17 at 09:41
  • actually it is important otherwise the app crashes – Zahraa Fatma Aug 25 '17 at 10:24

1 Answers1

0

First of all do not construct queries like that. This is very wrong habit which will made you in future to write code which is vulnerable to SQL injections.

Use rawQuery (String sql, String[] selectionArgs). So it should look like this:

Cursor c = db.rawQuery("SELECT * FROM attendance where user_id = ? and arrival_time = ? and departure_time IS NULL", 
                       new String[] { check_id, dateoftoday} );

Also take a look on this SO answer. And this answer looks even better - store dates as number of milliseconds since EPOCH time.

Marek R
  • 32,568
  • 6
  • 55
  • 140