我有一个奇怪的错误,即FDQuery在函数中多次调用时抛出访问冲突。基本上,我有一个函数,它循环通过一个日期范围来产生一个从SQL表中提取的数据的2D数组。
这个函数调用了另一个函数,它的工作是生成一个一天的数组。该函数以这种方式调用:
while (date <= endDate) do
begin
try
try
FDQuery := TFDQuery.Create(nil);
FDQuery.Connection := FDConnection;
ConvertData(date, DataTableName, iMaxVarIndex, iSiteID, FDQuery, DataArray,
sCommentsArray);
finally
if assigned(FDQuery) then FDQuery.Free;
end;
//for loop filling the 2d array...
date := System.DateUtils.IncDay(date, 1);
Inc(d);
except
on E: Exception do
begin
Result := false;
break;
end;
end;
end;
ConvertData的实现是这样的:
procedure ConvertData(args);
var
//var decl
begin
if not assigned(queryObj) then
raise Exception.Create('Query object is unassigned.');
with queryObj do
begin
// Prepare the SQL statement (obfuscated for demonstration)
SQL.Text := 'SELECT col1, col2, col3, col4, col5, col6 FROM ' + tableName + ' WHERE dateColumn=:param1 AND siteColumn=:param2';
ParamByName('param1').AsDate := dateParam;
ParamByName('param2').AsInteger := siteID;
try
Open;
if (Recordcount = 0) then exit;
except
on E: Exception do
begin
ShowMessage('Error executing query: ' + E.Message);
Exit;
end;
end;
end;
try
// Create and initialize memory streams
commentsStream := TMemoryStream.Create;
dataStream := TMemoryStream.Create;
attrStream := TMemoryStream.Create;
try
// Copy blob data to memory streams (obfuscated for demonstration)
commentsBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col4'), bmRead);
if assigned(commentsBlob) then
commentsStream.CopyFrom(commentsBlob, queryObj.FieldByName('col1').AsInteger);
dataBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col5'), bmRead);
if assigned(dataBlob) then
dataStream.CopyFrom(dataBlob, queryObj.FieldByName('col2').AsInteger);
attrBlob := queryObj.CreateBlobStream(queryObj.FieldByName('col6'), bmRead);
if assigned(attrBlob) then
attrStream.CopyFrom(attrBlob, queryObj.FieldByName('col3').AsInteger);
// Convert streams to array
with queryObj do
begin
Close;
end;
SetLength(attrArray, maxIndex);
for counter1 := Low(attrArray) to High(attrArray) do
attrArray[counter1] := NaN;
ConvertStreamToData(attrStream, attrArray, tempNumericArray);
SetLength(dataArray, maxIndex + maxIndex);
SetLength(commentsArray, maxIndex + maxIndex);
for counter1 := Low(dataArray) to High(dataArray) do
dataArray[counter1] := NaN;
for counter2 := Low(commentsArray) to High(commentsArray) do
commentsArray[counter2] := '';
ConvertCommentsStream(commentsStream, commentsArray, tempStringArray);
ConvertDataStream(dataStream, dataArray, tempNumericArray);
// Additional processing
dateObj := SomeUtility.DecodeDateFunction(dateParam);
for loopCounter := Low(attrArray) to High(attrArray) do
begin
dataArray[maxIndex + loopCounter] := attrArray[loopCounter];
end;
finally
// Free the memory streams
commentsStream.Free;
dataStream.Free;
attrStream.Free;
end;
except
on E: Exception do
begin
ShowMessage('Error in ConvertData: ' + E.Message);
end;
end;
end;
访问冲突发生在我打开、关闭或释放FDQuery的行上,并且似乎总是在第二次调用函数时发生。尽管查询在每次循环迭代中被创建、打开、关闭和释放,但它仍然会抛出这些异常,就好像对象无效一样。
10年前我也有类似的问题,可以在here上找到,尽管解决方案不起作用或没有意义。
感谢您的帮助!
1条答案
按热度按时间6ss1mwsb1#
以下是一些问题和建议:
1.为什么要在while中创建和分配fdconnection?
1.你是否在某处关闭查询?
1.您的查询应在while之前创建。还要考虑到性能