Search code examples
c++builderdbgrid

FireDAC Query connect to DBGrid


I have FDQuery and DataSource and DBGrid. Now I write this code in Button

  FDQuery2->Active = false;
  FDQuery2->SQL->Clear();
  FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                  " FROM Checkup C "
                  " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                  " WHERE C.today = " + MaskEdit1->Text +
                  " ORDER BY C.check_id ");
  FDQuery2->Active = true;

and i connect FDQuery to DataSource and tDataSource to DBGrid, but when I click the Button it doesn't show rows. and i am sure that SQL code is work, because when i write inside the SQL String the rows have been shown. any ideas.


Solution

  • You're missing the ' around your value when concatenating the text. Change your WHERE clause:

    FDQuery2->SQL->Clear();
    FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                    " FROM Checkup C "
                    " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                    " WHERE C.today = '" + MaskEdit1->Text + "'" +
                    " ORDER BY C.check_id ");
    

    You really should learn to use parameterized queries instead, though. It allows the database driver to handle things like properly quoting text or formatting dates for you, and it also (importantly) prevents SQL injection.

    FDQuery2->SQL->Clear();
    FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                    " FROM Checkup C "
                    " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                    " WHERE C.today = :today" + 
                    " ORDER BY C.check_id ");
    FDQuery2->ParamByName("today")->AsString = MaskEdit1.Text;
    FDQuery2->Active = true;