逐行从备忘录框中提取文本

时间:2012-04-06 20:26:30

标签: database delphi paradox

我需要查看存储在备注字段中的paradox表中的大量数据。我需要逐行处理这个数据并处理每一行。

如何告诉Delphi逐个获取备忘录字段中的每一行?

我可以使用#13#10作为分隔符吗?

2 个答案:

答案 0 :(得分:4)

假设备注字段中的内容使用#13#10作为行分隔符,那么我将使用TStringList和非常有用的Text属性将备注字段文本拆分为单独的行:

var
  StringList: TStringList;
  Line: string;
.....
StringList.Text := MemoFieldText;
for Line in StringList do
  Process(Line);

即使您的备注字段使用Unix换行符,此代码也会正确解释备忘录字段。

答案 1 :(得分:1)

这取决于在Paradox中实际声明字段的方式。如果它是TMemoField,那很简单:

var
  SL: TStringList;
  Line: string;
begin
  SL := TStringList.Create;
  try
    SL.Text := YourMemoField.GetAsString;
    for Line in SL do
     // Process each line of text using `Line`
  finally
    SL.Free;
  end;
end;

如果它是TBlobField,那就复杂一点了。您需要使用TBlobStream阅读备忘录字段,并将该流的内容加载到TStringList

// For Delphi versions that support it:
procedure LoadBlobToStringList(const DS: TDataSet; const FieldName: string;
  const SL: TStringList);
var
  Stream: TStream;
begin
  Assert(Assigned(SL), 'Create the stringlist for LoadBlobToStringList!');
  SL.Clear;
  Stream := DS.CreateBlobStream(DS.FieldByName(FieldName), bmRead);
  try
    SL.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

// For older Delphi versions that do not have TDataSet.CreateBlobStream
procedure LoadBlobToStringList(const DS: TDataSet; const TheField: TField; 
  const SL: TStringList);
var
  BlobStr: TBlobStream;
begin
  Assert(Assigned(SL), 'Create the stringlist for LoadBlobToStringList!');
  SL.Clear;
  BlobStr := TBlobStream.Create(DS.FieldByName(TheField), bmRead);
  try
    SL.LoadFromStream(BlobStr);
  finally
    BlobStr.Free;
  end;
end;

// Use it
var
  SL: TStringList;
  Line: string;
begin
  SL := TStringList.Create;
  LoadBlobToStringList(YourTable, YourMemoFieldName, SL);
  for Line in SL do
    // Process each Line, which will be the individual line in the blob field

  // Alternatively, for earlier Delphi versions that don't support for..in
  // declare an integer variable `i`
  for i := 0 to SL.Count - 1 do
  begin
    Line := SL[i];
    // process line of text using Line
  end;
end;