Variant or safe array is locked - Iterating Recordset


2004-06-07 07:39:10 PM
delphi61
Hi all.
I'm comparing the speed of different ways to acomplish the task of reading
two sets of data from a database, loop the sets and put the result into a
third set of data and save this back to the database.
I now has encounterd a problem I can not find any help on. Given the code
below I get an exception with the message "Variant or safe array is locked".
Help please.
/Stefan Olofsson
type
PArrayData = ^TArrayData;
TArrayData = array of array of OleVariant;
----Cut----
procedure TForm4.TestADO(Rows: integer);
var
Q1, Q2 : TadvQuery;
Q1Var, Q2var : OleVariant;
Q1Loop, Q2Loop : integer;
Data1, Data2 : PArrayData;
begin
Q1 := TadvQuery.Create(nil);
Q2 := TadvQuery.Create(nil);
try
Q1.Connection := ADOConnection1;
Q2.Connection := ADOConnection1;
Q1.SQL.Text := 'select top ' + IntToStr(Rows) + ' Col1, Col2, Col3 from
Test order by Col1';
Q1.Open;
Q1Var := Q1.Recordset.GetRows(Q1.RecordCount, EmptyParam, EmptyParam);
Data1 := VarArrayLock(Q1Var);
Q1.Close;
Q2.SQL.Text := 'select top ' + IntToStr(Rows div 10) + ' Col1, Col2,
Col3 from Test order by Col1';
Q2.Open;
Q2Var := Q2.Recordset.GetRows(Q2.RecordCount, EmptyParam, EmptyParam);
Data2 := VarArrayLock(Q2Var);
Q2.Close;
for Q1Loop := 0 to Length(Data1^) - 1 do <------------------ Error
thrown here
for Q2Loop := 0 to Length(Data2^) - 1 do
if (Copy(Data1^[0,Q1Loop], 1, 5) = Copy(Data2^[0,Q2Loop], 1, 5)) or
(Copy(Data1^[0,Q1Loop], 1, 5) = Copy(Data2^[1,Q2Loop], 1, 5)) or
(Copy(Data1^[0,Q1Loop], 1, 5) = Copy(Data2^[2,Q2Loop], 1, 5))
then begin
Q3.AppendRecord([Data1^[0,Q1Loop],Data2^[1,Q2Loop],Data1^[2,Q1Loop]]);
Inc(Res.ResultRows);
end;
// This code without using VarArrayLock is working.
for Q1Loop := 0 to VarArrayHighBound(Q1Var, 2) do
for Q2Loop := 0 to VarArrayHighBound(Q2Var, 2) do
if (Copy(Q1Var[0,Q1Loop], 1, 5) = Copy(Q2Var[0,Q2Loop], 1, 5)) or
(Copy(Q1Var[0,Q1Loop], 1, 5) = Copy(Q2Var[1,Q2Loop], 1, 5)) or
(Copy(Q1Var[0,Q1Loop], 1, 5) = Copy(Q2Var[2,Q2Loop], 1, 5)) then
begin
Q3.AppendRecord([Q1Var[0,Q1Loop],Q2Var[1,Q2Loop],Q1Var[2,Q1Loop]]);
Inc(Res.ResultRows);
end;