【发布时间】:2015-11-18 12:27:37
【问题描述】:
我有一个包含在数据库表中使用的键列表的 Tstringlist。 我想要一种简单的方法来生成一个包含所有键的字符串,每个键用逗号分隔并用单引号括起来。 这样它就可以在 SQL 'IN' 语句中使用 例如WHERE FieldX IN ('One','Two','Three').
我尝试过使用quotechar,但在阅读 commatext 时它会被忽略。 比如下面的代码
procedure junk;
var
SL : Tstringlist;
s : string;
begin
SL := Tstringlist.Create;
SL.Delimiter :=','; //comma delimiter
SL.QuoteChar := ''''; //single quote around strings
SL.Add('One');
SL.Add('Two');
SL.Add('Three');
try
s := SL.commatext;
showmessage(s);
finally
SL.Free;
end; //finally
end; //junk
显示消息 One,Two,Three - 不带任何引号。
我知道我可以做到这一点,就像在
procedure junk;
var
SL : Tstringlist;
s : string;
i : integer;
begin
SL := Tstringlist.Create;
SL.Delimiter :=','; //comma delimiter
SL.Add('One');
SL.Add('Two');
SL.Add('Three');
try
s := '';
for I := 0 to SL.Count - 1 do
begin
s := s + ',' + '''' + SL[i] + '''';
end;
delete(s,1,1);
showmessage(s);
finally
SL.Free;
end;//finally
end;
但是有没有更简单的方法使用 Tstringlist 本身的属性?
【问题讨论】:
-
您这样做的尝试是不够的,因为您没有转义引号字符
标签: delphi csv tstringlist