lynx_rousse
11 years agoNew Contributor
Using a variable in SQL query in DelphyScrypt
What I need to do: I have an Excel file with a list of barcode. I need to take them one by one and then using an SQL query serch an Oracle Database for relevant information. To do this, I take a barcode from file, assign it to a variable and then enter the variable into the query. But TestComplete doesn't seem to understand what I want from it. here is the code:
procedure Main;
var AConnection, RecSet, Phone, Type, Type_name, stringconn, var1, ddtExcel;
begin
try
ddtExcel := DDT.ExcelDriver('C:/Barcode.xls', 'Sheet 1', true);
while not ddtExcel.EOF do
begin
var1 := ddtExcel.Value['barcode'];
// Create a Connection object
AConnection := ADO.CreateADOConnection;
stringconn:='a correct string that works';
// Suppress the login dialog box;
// Specify the connection string
AConnection.ConnectionString := stringconn;
AConnection.LoginPrompt := False;
AConnection.Open();
// Execute a query
RecSet := AConnection.Execute_
('select i.tel_number, i.sms_type from sms.sms_item t join sms.sms_info i on t.ID_SMS_ITEM=i.ID_SMS_ITEM where t.BARCODE='+ StrToInt(var1));
// Iterate through query results and insert data into the test log
RecSet.MoveFirst;
while not aqConvert.VarToBool(RecSet.EOF) do
begin
Log.AppendFolder(var1);
Log.AppendFolder(RecSet.Fields('TEL_NUMBER').Value);
Log.Message(RecSet.Fields('SMS_TYPE').Value);
RecSet.MoveNext;
Log.PopLogFolder;
end;
AConnection.Close;
end;
ddtExcel.Next();
end;
DDT.CloseDriver(ddtExcel.Name);
except
AConnection.Close;
Log.Error('Exception', ExceptionMessage)
end;
end;
I believe it will turn out as a simple syntax error, but I can't find a proper example of a syntax for such a case. Here on the forum I saw only examples with other languages...
procedure Main;
var AConnection, RecSet, Phone, Type, Type_name, stringconn, var1, ddtExcel;
begin
try
ddtExcel := DDT.ExcelDriver('C:/Barcode.xls', 'Sheet 1', true);
while not ddtExcel.EOF do
begin
var1 := ddtExcel.Value['barcode'];
// Create a Connection object
AConnection := ADO.CreateADOConnection;
stringconn:='a correct string that works';
// Suppress the login dialog box;
// Specify the connection string
AConnection.ConnectionString := stringconn;
AConnection.LoginPrompt := False;
AConnection.Open();
// Execute a query
RecSet := AConnection.Execute_
('select i.tel_number, i.sms_type from sms.sms_item t join sms.sms_info i on t.ID_SMS_ITEM=i.ID_SMS_ITEM where t.BARCODE='+ StrToInt(var1));
// Iterate through query results and insert data into the test log
RecSet.MoveFirst;
while not aqConvert.VarToBool(RecSet.EOF) do
begin
Log.AppendFolder(var1);
Log.AppendFolder(RecSet.Fields('TEL_NUMBER').Value);
Log.Message(RecSet.Fields('SMS_TYPE').Value);
RecSet.MoveNext;
Log.PopLogFolder;
end;
AConnection.Close;
end;
ddtExcel.Next();
end;
DDT.CloseDriver(ddtExcel.Name);
except
AConnection.Close;
Log.Error('Exception', ExceptionMessage)
end;
end;
I believe it will turn out as a simple syntax error, but I can't find a proper example of a syntax for such a case. Here on the forum I saw only examples with other languages...