0

I am new to Interbase in delphi. Could anyone show me how to properly use Interbase to query data from a Firebird database in coding? I have tried something like this:

procedure TfJM00001.af_search;
var
  i        : integer;
  qryQuery : TIBdataset;
  sql : string;
 begin
   Try
    qryQuery := TIBDataSet.Create(nil);
    qryQuery.Database := IBDatabase1;
    qryQuery.Transaction := qryQuery.Database.DefaultTransaction;

  sql := ' select pro.pro_id, pro.pro_nm, cat.cat_nm, div.division_nm,      pro.price, pro.weight, pro.qty , typ.type_nm, pro.images ' + #13#10 +
     ' from product pro ' +#13#10+
     ' left join division div on div.division_id = pro.division_id ' +#13#10+
     ' left join category cat on cat.cat_id = pro.cat_id ' +#13#10+
     ' left join tbl_type typ on typ.type_id = pro.type_id; ' ;

try

  qryQuery.Close;
  qryQuery.SelectSQL.Text := sql;
  qryQuery.Open;

  if qryQuery.RecordCount < 0 then exit;

  with cxGrid1BandedTableView1.DataController do
  begin
    RecordCount := 0;
    BeginUpdate;
      while not(qryQuery.Eof) do
      begin
        i := AppendRecord;

        SetValue( i, col_Pro_Id.Index         ,qryQuery.FieldByName('PRO_ID').AsString);
        SetValue( i, col_Pro_NM.Index         ,qryQuery.FieldByName('PRO_NM').AsString);
        SetValue( i, col_Cat_NM.Index         ,qryQuery.FieldByName('CAT_NM').AsString);
        SetValue( i, col_Type_NM.Index        ,qryQuery.FieldByName('TYPE_NM').AsString);
        SetValue( i, col_Price.Index          ,qryQuery.FieldByName('PRICE').AsString);
//        SetValue( i, Col_Description.Index    ,qryQuery.FieldByName('PRO_NM').AsString);
        SetValue( i, Col_Weight.Index         ,qryQuery.FieldByName('WEIGHT').AsString);
        SetValue( i, col_Qty.Index            ,qryQuery.FieldByName('QTY').AsString);
        SetValue( i, col_Division.Index       ,qryQuery.FieldByName('DIVISION_NM').AsString);
//        SetValue( i, col_register_date.Index  ,qryQuery.FieldByName('PRO_NM').AsString);
        //SetValue( i, col_Compound.Index       ,qryQuery.FieldByName('Images').AsVariant);
        qryQuery.Next;
      end;
    EndUpdate;
  end;
except on
 e : exception do
 begin
  showmessage('Error Message : ' + e.Message);
 end;
end;
  Finally
    qryQuery.Database.DefaultTransaction.Active := False;
  end;
end;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Chhun Panharath
  • 136
  • 1
  • 10
  • 2
    SO is about **specific** questions. What **exact** problem are you having? – MartynA Jul 04 '16 at 08:33
  • 3
    Firebird != Interbase. Is your question really about Interbase or about Firebird, because those two are not (no longer) the same and you can't use Interbase to query a Firebird database. – Mark Rotteveel Jul 04 '16 at 09:01
  • I have database which is a (*.fdb) and in my program i used interbase component to query data from (*.fdb) is it okay to do that ? or is there any other to achieve this? – Chhun Panharath Jul 04 '16 at 09:06
  • The exact problem is it works for me with the above code. but there is problem when i close the form. it takes long time to close. – Chhun Panharath Jul 04 '16 at 09:08
  • 3
    Then either that `fdb` is a really old database (eg Firebird 1 / ODS 10), or it is just named `fdb` and it is really an Interbase database. In any case, if you have a problem, then you should describe that problem in detail in your question (with error messages if available). Making us guess, or providing those details only in comments is not how you should use Stackoverflow. – Mark Rotteveel Jul 04 '16 at 09:36
  • I'm afraid that atm, this is a very poor quality question because it contains so little information relevant to the problem you are trying to solve. For starters, what TDataSet-descendant types are your qryQuery and the dataset connected to your cxGrid1BandedTableView1.DataController.? Also, is the data in the latter dataset persisted anywhere after your form is closed? – MartynA Jul 04 '16 at 17:31
  • @MarkRotteveel I think his question is about IBX - "Interbase Express" components, EMBA fork of FIBC – Arioch 'The Jul 04 '16 at 21:10
  • do not call `qryQuery.FieldByName` in the loop!!! it is very slow! call it BEFORE the loop and cache results in `TField` typed local variables – Arioch 'The Jul 04 '16 at 21:11

1 Answers1

0

You should hook the Query up to a Datasource, use a DBBandedTableView and link the DBBandedTableView to the Query. The cxGrid1BandedTableView1.DataController is not meant to be populated like this.

Birger
  • 4,343
  • 21
  • 35