mysql - 在delphi 7上用数组检索多个记录值

标签 mysql arrays delphi delphi-7

我目前正在做一个小项目,并陷入了这个问题。 因此,我创建了一个包含项目详细信息的临时表,我想要的是当我按下按钮[保存]时,它将检索这些记录的特定值并将这些值放入不同的临时变量中。

这就是我为选择特定记录值所做的事情。

DM.Zread.close;
DM.Zread.SQL.Commatext:= 'Select id_dvd from temp_table where rent_id="rent-0001"';
DM.Zread.open;

例如: 选定的记录值:

id_dvd
| 2 |
| 6 |
| 5 |

正如我之前所说,我想将这些记录值放入 3 个不同的临时变量中。 所以它会像:

//assumed i did the array function

a:=x[0]; // this line contain value from record one which is 2
b:=x[1]; // this line contain value from record two which is 6
c:=x[2]; // this line contain value from record three which is 5

我完全不知道该怎么做,我在 stackoverflow 上搜索过,没有任何讨论这个问题的线程。但我很少提到这个方法应该使用 Array 函数,这就是我使用数组制作示例的原因。老实说,我不太理解如何使用 Array

非常感谢帮助我解决这个问题的人。

谢谢

-丹

最佳答案

由于您不知道查询将返回多少行,因此您应该创建一个动态数组来保存值,然后在打开表后,循环记录并填充数组:

var
  i: integer;
  IDs: array of integer;
begin
  DM.Zread.close;
  DM.Zread.SQL.Text:= 
      Format('Select id_dvd from temp_table where rent_id=%s',[QuotedStr('rent-0001')]);
  DM.Zread.open;

  SetLength(IDs, DM.Zread.RecordCount);
  i:=0;
  while not DM.Zread.Eof do begin
    IDs[i] := DM.Zread.FieldByName('id_dvd').AsInteger;
    inc(i);
    DM.Zread.Next;
  end;
end;

关于mysql - 在delphi 7上用数组检索多个记录值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23896491/

相关文章:

PHP、SQL新手帮助

php - 如何将 CSV 解析为以第一个值作为键的数组?

ruby-on-rails - Ruby 将两个数组合并为一个

delphi - 如何在单击按钮时动态地将组件添加到 TScrollBox 中?

mysql - 使用加载数据将 CSV 文件导入 MySQL

MySql 无需停机即可迁移到 Aurora

php - 如何纠正mysql的字体错误

c++ - 解压缩中如何处理数组分配?

windows - SCardEstablishContext 作为服务挂起

Delphi:现场应用程序错误日志记录