delphi 添加带块的TStringList时遇到问题

jutyujz0  于 2023-03-18  发布在  其他
关注(0)|答案(2)|浏览(148)

我尝试将来自数据集(Q)的数据分成10个块,并将它们放入TStringList中。
我创建了一个名为LISTTStringList,其中每次找到记录时都会添加一个值(UID)。
我尝试过使用TStringList数组来跟踪,但这有点麻烦。
例如,假设数据集传递24条记录,LIST可以在LIST.strings[0]中具有前10个值,在LIST.Strings[1]中具有记录10到20,并且在LIST.Strings[2]中具有记录21、22和23。
你有什么想法或方向吗?我也试过了,但还是不行。我以后要不要处理LIST

const chunks = 10
List := TStringList.Create;   
Q.Open;
try
  While not Q.EOF Do
  begin
    UID := Q.FieldByname('U_ID');
    List.Add(UID.AsString);
    Q.Next;
  end;
  Q.Close;
finally
  FreeAndNil(Q);
end;
//SO DESIRED OUTPUT SOMETHING LIKE THIS  
//LIST.COUNT =3
//LIST.STRINGS[0] = 1,2,3,4,5,6,7,8,9,10  
//LIST.STRINGS[1] = 11,12,13,14,15,16,17,18,19,20 
//LIST.STRINGS[2] = 21,22,23

我也尝试过使用Insert(),如下所示:

List.QuoteChar := ' ';
if List.Count <= 10 then
begin
  MaxList.Insert(0, List.DelimitedText);
end;
if ((List.Count > 10) and (List.Count <= 20)) then
begin
  MaxList.Insert(1, List.DelimitedText);

但是Insert()索引并不像我想要的那样工作。

r3i60tvu

r3i60tvu1#

如果我没有理解错的话,您的目标是将UID存储为逗号分隔的字符串,其中每个字符串最多可以保存10个UID。

const
  chunks = 10;

var
  List: TStringList;
  counter: Integer;
begin
  ...
  counter := 0;
  List := TStringList.Create;   
  try
    Q.Open;
    try
      if not Q.EOF then
      begin
        UID := Q.FieldByName('U_ID');
        repeat
          if (counter mod chunks) = 0 then
            List.Add(UID.AsString)
          else
            List.Strings[List.Count-1] := List.Strings[List.Count-1] + ',' + UID.AsString;
          Inc(counter);
          Q.Next;
        until Q.EOF;
      end;
    finally
      Q.Close;
    end;
    // use List as needed...
  finally
    List.Free;
  end;
  ...
end;

或者,您可以使用2 TStringList代替计数器,例如:

const
  chunks = 10;

var
  List: TStringList;
  Chunk: TStringList;
begin
  ...
  List := TStringList.Create;   
  try
    Q.Open;
    try
      if not Q.EOF then
      begin
        UID := Q.FieldByName('U_ID');
        Chunk := TStringList.Create;   
        try
          repeat
            if Chunk.Count = chunks then
            begin
              List.Add(Chunk.CommaText);
              Chunk.Clear;
            end;
            Chunk.Add(UID.AsString);
            Q.Next;
          until Q.EOF;
          if Chunk.Count > 0 then
            List.Add(Chunk.CommaText);
        finally
          Chunk.Free;
        end;
      end;
    finally
      Q.Close;
    end;
    // use List as needed...
  finally
    List.Free;
  end;
  ...
end;
zysjyyx4

zysjyyx42#

使用帮助器字符串列表的类似方法:

List := TStringList.Create;
  try
    Q.Open;
    try
      line := TStringList.Create;
      try
        cnt := chunks;
        While not Q.EOF Do
        begin
          Dec(cnt);
          if cnt = 0 then begin
            List.Add(line.CommaText);
            line.Clear;
            cnt := chunks;
          end;
          UID := Q.FieldByname('U_ID');
          line.Add(UID.AsString);
          Q.Next;
        end;
        if line.Count > 0 then
          List.Add(line.CommaText);
      finally
        line.Free;
      end;
      Q.Close;
    finally
      Q.Free;
    end;
    // SO DESIRED OUTPUT SOMETHING LIKE THIS
    // LIST.COUNT =3
    Assert(List.Count = 3);
    // LIST.STRINGS[0] = 1,2,3,4,5,6,7,8,9,10
    Assert(List[0] = '1,2,3,4,5,6,7,8,9,10');
    // LIST.STRINGS[1] = 11,12,13,14,15,16,17,18,19,20
    Assert(List[1] = '11,12,13,14,15,16,17,18,19,20');
    // LIST.STRINGS[2] = 21,22,23
    Assert(List[2] = '21,22,23');
  finally
    List.Free;
  end;

相关问题