Delphi – 使用 FireDac 将 CSV 加载到数据集中(字段大小问题…)

huangapple go评论100阅读模式
英文:

Delphi - Loading a CSV into a Dataset using FireDac ( Field Sizing Issue... )

问题

I'm using TFDBatchMove, TFDBatchMoveTextReader, TFDBatchMoveDataSetWriter, and TFDMemTable to load data from a csv file into a memTable dataset. It works great except for the fact that I have one field that has a lot of text (400-500 characters) and for some reason the memTable component caps the field size at 233 characters... The fields are all loaded by the batch components and I can't find an option to extend the field size limit and same with the memTable. How do I get around this?

我正在使用TFDBatchMove、TFDBatchMoveTextReader、TFDBatchMoveDataSetWriter和TFDMemTable来从CSV文件加载数据到MemTable数据集中。除了一个字段有很多文本(400-500个字符)之外,它工作得很好,但由于某种原因,MemTable组件将字段大小限制在233个字符... 所有字段都由批处理组件加载,我找不到扩展字段大小限制的选项,MemTable也一样。我该如何解决这个问题?

英文:

I'm using TFDBatchMove, TFDBatchMoveTextReader, TFDBatchMoveDataSetWriter, and TFDMemTable to load data from a csv file into a memTable dataset. It works great except for the fact that I have one field that has a lot of text (400-500 characters) and for some reason the memTable component caps the field size at 233 characters... The fields are all loaded by the batch components and I can't find an option to extend the field size limit and same with the memTable. How do I get around this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  FMX.Edit, FMX.Controls.Presentation, FMX.StdCtrls, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Comp.BatchMove.DataSet,
  FireDAC.Comp.BatchMove, FireDAC.Comp.BatchMove.Text, System.Rtti,
  FMX.Grid.Style, FMX.ScrollBox, FMX.Grid, FireDAC.UI.Intf, FireDAC.FMXUI.Wait,
  FireDAC.Comp.UI, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Fmx.Bind.Grid,
  System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Components,
  Data.Bind.Grid, Data.Bind.DBScope;

type
  TForm1 = class(TForm)
    BatchMove: TFDBatchMove;
    csvReader: TFDBatchMoveTextReader;
    datasetWriter: TFDBatchMoveDataSetWriter;
    memTable: TFDMemTable;
    btnConvert: TButton;
    FilePath: TEdit;
    StringGrid1: TStringGrid;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    LinkGridToDataSourceBindSourceDB1: TLinkGridToDataSource;
    procedure btnConvertClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.btnConvertClick(Sender: TObject);
begin
  csvReader.FileName := FilePath.Text;
  BatchMove.Execute;
end;

<!-- end snippet -->

答案1

得分: 1

找到了一个解决办法。即使我将AnalyzeSample大小设置为包括整个数据集,由于某种原因,TFDBatchMove无法找出正确的字段大小以适应数据集中的每条记录,并将其设置得太低。我无法让它起作用。然而,通过在运行时手动创建我的TFDMemTable中的所有FieldDefs,我可以将字段大小定义为我想要的任何大小。然后通过取消选中“poCreateDest”选项,TFDBatchMove将不再尝试根据自己的分析来格式化字段,而是直接将数据写入我已经创建的字段中。这样做的动态性较弱,因为字段参数是固定的,但对于我需要它做的事情来说,它足够好用。

英文:

Figured out a way around it. Even if I set the AnalyzeSample size to encompass the entire dataset, for some reason the TFDBatchMove was not able to figure out the correct field size to fit every record in the dataset and was setting it way too low. I was unable to get this to work. However, by creating all of the FieldDefs in my TFDMemTable manually (at runtime), I could define the field sizes to be whatever I wanted them to be. Then by unchecking the "poCreateDest" option, the TFDBatchMove would no longer try to format the fields based off of its own analysis and instead just write data to the fields that I had already created. It's less dynamic this way because the field parameters are fixed but it works well enough for what I need it to do.

huangapple
  • 本文由 发表于 2023年7月18日 00:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定