如何在 Pascal 中向字符串添加单引号(')或双引号(“)?
How to add single(') or double(") quotes to a String in pascal?
我正在尝试将引号 "
或 '
添加到字符串列表中,这将在 SQL 查询中使用。系统使用的是 Firebird 2.5.
此查询有一个 not in
子句,我正在寻找具有以下格式的查询:
Select c.codes
From Table c
where c.cuit not in ("20-11111111","20-11111111","20-11111111".....to n codes)
此过程从网格加载字符串列表:
function loadCodesFromGrid(): WideString;
var
slAux: TStringList;
wsAux : WideString;
stTable: String;
qCliProv: TFXQuery;
niCol : Integer;
begin
niCol := 1;
for n := 1 to gDetails.RowCount - 1 do //load cuit from grid. The cuit in my country is like the social security number in United States
if Trim(gDetails.Cells[niCol, n]) <> '' then
wsAux := wsAux + iif((Trim(wsAux) = ''), '', ',') + Trim(gDetails.Cells[3, n]);
slAux := fxFormatQuery(wsAux );
try
qCliProv.SQL.Text :=
' Select Code' + iif(pboClients, Copy(stTable, 0, 7), Copy(stTable, 0, 9)) + ' As Code' +
' From ' + stTable +
' Where Active = 1 ';
if slAux.Count > 0 then
for n := 0 to slAux.Count - 1 do
begin
if Trim(slAux.Strings[n]) = '' then
Continue;
qCliProv.SQL.Add(
' And Cuit Not In (' + slAux.Strings[n] + ')' );
end;
qCliProv.Open;
//the rest of the code is not important
此函数格式化查询:
function fxFormatQuerySQL(pstClients: WideString): TStringList;
var
slAux, slAuxResult: TStringList;
niI, niLine: Integer;
begin
niLine := 0;
slAuxResult := TStringList.Create;
slAux := TStringList.Create;
try
slAuxResultado.Add('"'); //
slAux.Delimiter := ',';
slAux.DelimitedText := pstClients;
for niI := 0 to slAux.Count - 1 do
begin
if ((Frac(niI/100) = 0) and (nII <> 0)) then
begin
Inc(niLine);
slAuxResult.Add('');
end;
slAuxResult.Strings[niLine] := slAuxResult.Strings[niLine] +
iif((slAuxResult.Strings[niLine] = ''), '', ',' ) + slAux.Strings[niI];
end;
Result := slAuxResult;
finally
FreeAndNil(slAux);
end;
end;
我尝试了很多更改,但无法以我想要的格式生成查询。
只需将您在 fxFormatQuerySQL
中生成的元素引用为
+ AnsiQuotedStr(slAux.Strings[niI], '"')
而不是
+ slAux.Strings[niI];
您没有正确格式化 not in
子句。而且您正在对每个网格行单独执行 SQL 查询,这完全违背了使用 not in
子句的目的。
您也根本不需要 fxFormatQuery()
函数。您可以使用 RTL 的 AnsiQuotedStr()
函数为您处理引号。
尝试更像这样的东西:
function loadCodesFromGrid(): String;
var
slAux: TStringList;
wsAux, stTable: String;
qCliProv: TFXQuery;
I: Integer;
begin
...
slAux := TStringList.Create;
try
for I := 1 to gDetails.RowCount - 1 do
begin
if Trim(gDetails.Cells[1, I]) <> '' then
begin
wsAux := Trim(gDetails.Cells[3, I]);
if wsAux <> '' then
slAux.Add(AnsiQuotedStr(wsAux, '"'));
end;
end;
qCliProv.SQL.Text :=
' Select Code' + Copy(stTable, 0, iif(pboClients, 7, 9)) + ' As Code' +
' From ' + stTable +
' Where Active = 1 ';
if slAux.Count > 0 then
begin
slAux.Delimiter := ',';
slAux.QuoteChar := #0;
qCliProv.SQL.Add(
' And Cuit Not In (' + slAux.DelimitedText + ')' );
end;
finally
slAux.Free;
end;
qCliProv.Open;
...
end;
我正在尝试将引号 "
或 '
添加到字符串列表中,这将在 SQL 查询中使用。系统使用的是 Firebird 2.5.
此查询有一个 not in
子句,我正在寻找具有以下格式的查询:
Select c.codes
From Table c
where c.cuit not in ("20-11111111","20-11111111","20-11111111".....to n codes)
此过程从网格加载字符串列表:
function loadCodesFromGrid(): WideString;
var
slAux: TStringList;
wsAux : WideString;
stTable: String;
qCliProv: TFXQuery;
niCol : Integer;
begin
niCol := 1;
for n := 1 to gDetails.RowCount - 1 do //load cuit from grid. The cuit in my country is like the social security number in United States
if Trim(gDetails.Cells[niCol, n]) <> '' then
wsAux := wsAux + iif((Trim(wsAux) = ''), '', ',') + Trim(gDetails.Cells[3, n]);
slAux := fxFormatQuery(wsAux );
try
qCliProv.SQL.Text :=
' Select Code' + iif(pboClients, Copy(stTable, 0, 7), Copy(stTable, 0, 9)) + ' As Code' +
' From ' + stTable +
' Where Active = 1 ';
if slAux.Count > 0 then
for n := 0 to slAux.Count - 1 do
begin
if Trim(slAux.Strings[n]) = '' then
Continue;
qCliProv.SQL.Add(
' And Cuit Not In (' + slAux.Strings[n] + ')' );
end;
qCliProv.Open;
//the rest of the code is not important
此函数格式化查询:
function fxFormatQuerySQL(pstClients: WideString): TStringList;
var
slAux, slAuxResult: TStringList;
niI, niLine: Integer;
begin
niLine := 0;
slAuxResult := TStringList.Create;
slAux := TStringList.Create;
try
slAuxResultado.Add('"'); //
slAux.Delimiter := ',';
slAux.DelimitedText := pstClients;
for niI := 0 to slAux.Count - 1 do
begin
if ((Frac(niI/100) = 0) and (nII <> 0)) then
begin
Inc(niLine);
slAuxResult.Add('');
end;
slAuxResult.Strings[niLine] := slAuxResult.Strings[niLine] +
iif((slAuxResult.Strings[niLine] = ''), '', ',' ) + slAux.Strings[niI];
end;
Result := slAuxResult;
finally
FreeAndNil(slAux);
end;
end;
我尝试了很多更改,但无法以我想要的格式生成查询。
只需将您在 fxFormatQuerySQL
中生成的元素引用为
+ AnsiQuotedStr(slAux.Strings[niI], '"')
而不是
+ slAux.Strings[niI];
您没有正确格式化 not in
子句。而且您正在对每个网格行单独执行 SQL 查询,这完全违背了使用 not in
子句的目的。
您也根本不需要 fxFormatQuery()
函数。您可以使用 RTL 的 AnsiQuotedStr()
函数为您处理引号。
尝试更像这样的东西:
function loadCodesFromGrid(): String;
var
slAux: TStringList;
wsAux, stTable: String;
qCliProv: TFXQuery;
I: Integer;
begin
...
slAux := TStringList.Create;
try
for I := 1 to gDetails.RowCount - 1 do
begin
if Trim(gDetails.Cells[1, I]) <> '' then
begin
wsAux := Trim(gDetails.Cells[3, I]);
if wsAux <> '' then
slAux.Add(AnsiQuotedStr(wsAux, '"'));
end;
end;
qCliProv.SQL.Text :=
' Select Code' + Copy(stTable, 0, iif(pboClients, 7, 9)) + ' As Code' +
' From ' + stTable +
' Where Active = 1 ';
if slAux.Count > 0 then
begin
slAux.Delimiter := ',';
slAux.QuoteChar := #0;
qCliProv.SQL.Add(
' And Cuit Not In (' + slAux.DelimitedText + ')' );
end;
finally
slAux.Free;
end;
qCliProv.Open;
...
end;