gpt4 book ai didi

mysql - Delphi:如何将表(mysql)的内容加载到Float数组[1..50]中?

转载 作者:行者123 更新时间:2023-11-29 08:04:08 25 4
gpt4 key购买 nike

我在使用 MyDAC 的数据源模块中有一个表。我想将表的内容(包含 50 行的月份列)加载到 Float 数组 [1..50] 中。我怎样才能做到这一点?

这是我的数据模块单元的代码:

unit Unit2;

interface

uses
System.SysUtils, System.Classes, Data.DB, DBAccess, MyAccess, MemDS;

type
TDataModule2 = class(TDataModule)
MyConnection1: TMyConnection;
MyQuery1: TMyQuery;
MyQuery1Months: TFloatField;
MyQuery1Qob: TFloatField;
MyQuery1Qcalc: TFloatField;
MyDataSource1: TMyDataSource;
MyTable1: TMyTable;
private
{ Private declarations }
public
{ Public declarations }
end;

var
DataModule2: TDataModule2;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

end.

最佳答案

您似乎有一个包含 3 列的表,每列的类型均为 Float。 (尽管我忍不住想知道为什么名为“months”的列会包含 float 据。)您不会将该数据加载到包含 50 个浮点的数组中;而是将其加载到包含 float 的数组中。您可以将其加载到包含 3 个字段的 50 条记录或对象的数组中。

但是假设您只想加载其中一个字段的值。事情会是这样的:

i := 1; //index variable into the array
myQuery1.Open; //run the database query
while not myQuery1.EOF do //loop until we reach the end of the dataset
begin
myArray[i] := MyQuery1Qcalc.value; //read the field value to the array
myQuery1.Next; //next record
inc(i); //next array index
end;

请注意,如果此结果集包含的记录数与预期的 50 条记录数不同,您就会遇到麻烦。最好使用动态数组,并在调用 Open 后将其长度设置为数据集的 RecordCount 属性。但这是加载数据的基本模式:打开数据集(如果已经打开,则对其调用 First),然后从字段中读取值并调用 Next 直到您到达EOF(文件结束,在本例中实际上意味着记录集结束。)

关于mysql - Delphi:如何将表(mysql)的内容加载到Float数组[1..50]中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com