简体   繁体   English

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

[英]Delphi: How to load the contents of the table (mysql) into an Array [1..50] of Float?

I have a table in a datasource module using MyDAC. 我在使用MyDAC的数据源模块中有一个表。 I want to load the contents of the table, column months with 50 rows into an Array [1..50] of Float. 我想将具有50行的月份列的表的内容加载到Float的数组[1..50]中。 How can I do this? 我怎样才能做到这一点?

This is the code of my datamodule unit: 这是我的数据模块单元的代码:

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.

It appears that you have a table with 3 columns, each of which is typed Float. 看来您有一个包含3列的表,每列的类型为Float。 (Though I can't help but wonder why a column called "months" would contain floating-point data.) You wouldn't load that data into an array of 50 floats; (尽管我忍不住想知道为什么称为“ months”的列将包含浮点数据。)您不会将该数据加载到包含50个浮点数的数组中; you'd load it into an array of 50 records or objects containing 3 fields. 您会将其加载到包含3个字段的50条记录或对象的数组中。

But let's say you only wanted to load the values of one of the fields. 但是,假设您只想加载这些字段之一的值。 That would go something like this: 那会是这样的:

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;

Be aware that if this result set contains a different number of records than the expected 50, you'll have trouble. 请注意,如果此结果集包含的记录数与预期的50条记录不同,那么您将遇到麻烦。 It might be better to use a dynamic array and set its length equal to the dataset's RecordCount property after calling Open . 在调用Open之后,最好使用动态数组并将其长度设置为等于数据集的RecordCount属性。 But that's the basic pattern for loading data: open the dataset, (or call First on it if it's already open,) then read values from the fields and call Next until you reach the EOF (End Of File, which in this case actually means End Of Record Set.) 但这是加载数据的基本模式:打开数据集,(或者如果已经打开,则在其上调用First ),然后从字段中读取值,然后调用Next直到到达EOF (文件结束),这实际上意味着记录集的结尾。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM