我需要查看大量数据,这些数据存储在Memo字段中的paradox表中。我需要逐行处理此数据,并处理每一行。
如何告诉Delphi逐个获取memo字段中的每一行?
我可以使用#13#10作为分隔符吗?
发布于 2012-04-07 04:33:29
假设memo字段中的内容使用#13#10作为行分隔符,那么我将使用TStringList和非常有用的Text属性将memo字段文本拆分为单独的行:
var
StringList: TStringList;
Line: string;
.....
StringList.Text := MemoFieldText;
for Line in StringList do
Process(Line);即使您的备注字段使用Unix换行符,此代码也可以正确解释备注字段。
发布于 2012-04-07 04:52:15
这取决于字段在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读取memo字段,并将该流的内容加载到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;https://stackoverflow.com/questions/10048868
复制相似问题