从数据库中检索值

发布于 2025-01-23 04:52:07 字数 1352 浏览 7 评论 0原文

与我以前的问题 a>

我在StringGrid中成功将字符串值保存到我的数据库中。

现在,我想从数据库中检索值,并将其从StringGrid中恢复。

这是我尝试的:

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; 

它添加了行,但没有在数据库中获取值。

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2025-01-30 04:52:07

您缺少 first 下一个在查询对象的 tdataset 中移动的方法/em>
此代码可以工作:

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; 

当您使用SQL查询打开一组行(Delphi中的数据集)时的基本方法是这样:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文