Retrieve values from database to StringGrid

713 Views Asked by At

In connection with my previous question

I've successfully save string values in StringGrid to my database in one column.

Now I want to retrieved the values from database and put it back from StringGrid.

This is what I've tried :

procedure TForm1.BitBtn1Click(Sender: TObject);
var i, iRow, iCol: integer ;
s : string;
Grid2: TStringGrid;
strArray : Array of String;
charArray : Array[0..0] of Char;
begin
  getRecords;
  with SqlQuery4 do
  begin
    if RecordCount <> 0 then
    begin
      names := FieldByName('names').AsString;
      s := names;
      charArray[0] := '|';
      strArray     := s.Split(charArray);
      Grid2 := Form7.StringGrid1;
      i := 0;
      iCol:= 0;
      iRow:=0;
      for iRow := 1 to 19 do // increment rows
      begin
        for iCol := 0 to 5 do  // increment cols / max 5 cols
        begin
          if iCol = 5 then iCol := 0; // reset column so that it will go on next row            
          for i := 0 to Length(strArray)-1 do  // get string one by one
          begin
            Grid2.RowCount := Grid2.RowCount + 1; //  add row
            Grid2.Cells[iCol, iRow] := strArray[i]; // this value always overwrite, how to save this previous data?
          end;
        end;
      end;
    end;
  end;
end; 

It adds row but doesn't get the values in database..

1

There are 1 best solutions below

0
On

You are missing the First and Next methods for moving in a TDataSet of the query object SqlQuery4 This code could work:

procedure TForm1.BitBtn1Click(Sender: TObject);
var i, iRow, iCol: integer ;
s : string;
Grid2: TStringGrid;
strArray : Array of String;
charArray : Array[0..0] of Char;
begin
  getRecords;
  with SqlQuery4 do
  begin
    First; //SqlQuery4 goes to the first row returned by the SQL query
    if RecordCount <> 0 then
    begin
      names := FieldByName('names').AsString;
      s := names;
      charArray[0] := '|';
      strArray     := s.Split(charArray);
      Grid2 := Form7.StringGrid1;
      i := 0;
      iCol:= 0;
      iRow:=0;
      for iRow := 1 to 19 do // increment rows
      begin
        for iCol := 0 to 5 do  // increment cols / max 5 cols
        begin
          if iCol = 5 then iCol := 0; // reset column so that it will go on next row            
          for i := 0 to Length(strArray)-1 do  // get string one by one
          begin
            Grid2.RowCount := Grid2.RowCount + 1; //  add row
            Grid2.Cells[iCol, iRow] := strArray[i]; // this value always overwrite, how to save this previous data?
          end;
        end;
        Next; //SQLQuery goes to the next row available of the data set reurned by the SQL query
      end;
    end;
  end;
end; 

The basic way when you open a set of rows (a data set in Delphi) using a SQL query is something like this:

Procedure TForm1.ProcessData;
begin
  {
   The SQL query may look like:
   SELECT *
   FROM TABLE
   WHERE
     COLUMN1 = :PARAMETER1 AND /*Column1 is a VARCHAR */
     COLUMN2 = :PARAMETER2     /*Column2 is a Integer*/
  }
  //If already open, the close it
  if Query1.Active then
    Query1.Close;
  //Load some parameters from some components
  Query1.ParamByName('PARAMETER1').AsString := Edit1.Text;
  Query1.ParamByName('PARAMETER2').AsInteger := ComboBox1.ItemIndex;
  //Open the SQL query
  Query1.Open;
  Query1.First;  //We assure that the we are on the first record
  while Not Query1.Eof do  //Get into a whiile loop, exit when gets the end of the rows list returned by the Query1 object
  begin
    {
      Your processing code goes here
    }
    Query1.Next; //Now you advance to the next row to process it, if there is no more rows EOF become TRUE (EOF(stans for End Of File)
  end
  Query1.Close; //is a good idea to close the query if you not longer need it to save resources
end;