Help with Parameterized Query (using Delphi 7 and BDE) - delphi

Can someone help me and explain to my why this code crashes on the ExecSQL statement?
function UpdateLastBankResponsesId(ADatabase: TDatabase; AValue: Integer): String;
var
AQuery2: TQuery;
begin
result:= '';
AQuery2:= TQuery.Create(nil);
AQuery2.DatabaseName:= ADatabase.DatabaseName;
with AQuery2 do
begin
SQL.Text:= 'UPDATE last_id Set TABLENAME =:ATableName, LASTID=:ALastId';
ParamByName('ATableName').AsString:= 'responses';
ParamByName('ALastId').AsInteger:= AValue;
try
ExecSql; //***** CRASHES HERE *****
except
begin
ExitCode:= 16;
raise ECustomException.create('Error Updating Last Id table!');
end;//except
end; //try
end; //with
AQuery2.Free;
end;

I was trying to set a value that already existed
Set TABLENAME =:ATableName
I actually only needed to set the LASTID
UPDATE last_id SET LASTID=:ALastId WHERE TABLENAME =:ATableName
I can't believe I did that

Related

ADOQuery Get Result (Delphi)

I Tried to get result from ADOQuery in Delphi. I wrote this function for Get a Name from table according custom ID.
function GetNameByID(Id : Integer) : string;
var query : string;
Begin
ShowMessage(GetDBGridViewIndex().ToString);
query := 'SELECT Name FROM Table1 WHERE ID=' + IntToStr(Id);
With ADOQuery do
Begin
try
SQL.Clear;
SQL.Add(query);
Open;
First;
Result:= // Need Get Result;
finally
Close;
end;
End;
ShowMessage(result);
End;
But I don't know how can return Result from ADOQuery.
TADOQuery is a descendant of TDataset.
You can iterate through the result records with the First, Next, Prior, and Last methods and find out if you've reached the end with Eof.
Within each result record you can access the fields with:
Fields[Index].AsString
Fields[Index].AsInteger
...
or
FieldByName(FieldName).AsString
FieldByName(FieldName).AsInteger
...
In this case you can access to the result using:
Result := Fields[0].AsString;
Result := FieldByName('Name').AsString;
After you Open the query, the cursor is pointing the First record. You don't need to call the First method after Open.

ADO insert a record into access DB using delphi

Am learning how to use insert into statements and, with my access database, am trying to insert a single record. The table I'm inserting a new record into has three fields: StockID (AutoN), Description (Text), Cost (Number). I've looked at previous posts but the posted solutions seem to go beyond my basic level of Insert Into...which is what I'm interested in. Anyway, here is my code...
adoQuery1.Active := true;
adoQuery1.SQL.Clear;
adoQuery1.SQL.Add('INSERT INTO Stock (StockID,Description,Cost) VALUES (4,Cheese,5)');
adoQuery1.open;
adoQuery1.Close;
It compiles fine, but when press a command button to invoke the above, I get the following message:
'ADOQuery1: "Missing SQL property".'
what am I doing wrong?
Thanks, Abelisto. Your last post looks complex indeed...but I did my own little version since your last solution got me up and running. It works so I'm very chuffed. Am now going to focus on DELETE FROM using combobox (for field selection) and user value. Here was my solution I got working... ;)
x:=strtoint(txtStockID.Text);
y:=txtDescription.Text;
z:=strtoCurr(txtCost.Text);
adoQuery1.SQL.Clear;
adoQuery1.SQL.Add('INSERT INTO tblStock (StockID,Description,Cost)');
adoQuery1.SQL.Add('VALUES (:StockID,:Description,:Cost)'); // ':StockID' denotes a parameter
adoQuery1.Parameters.ParamByName('StockID').Value:= x;
adoQuery1.Parameters.ParamByName('Description').Value:= y;
adoQuery1.Parameters.ParamByName('Cost').Value:= z;
adoQuery1.ExecSQL;
adoQuery1.Close;
Using parameters is more efficient then constant SQL statements.
Additional to my comments here is some useful functions which I using frequently to call SQL statements with parameters (Maybe it will be useful for you too):
function TCore.ExecQuery(const ASQL: String; const AParamNames: array of string;
const AParamValues: array of Variant): Integer;
var
q: TADOQuery;
i: Integer;
begin
if Length(AParamNames) <> Length(AParamValues) then
raise Exception.Create('There are different number of parameter names and values.');
q := GetQuery(ASQL) as TADOQuery;
try
for i := Low(AParamNames) to High(AParamNames) do
SetParamValue(q, AParamNames[i], AParamValues[i]);
q.ExecSQL;
Result := q.RowsAffected;
finally
q.Free;
end;
end;
function TCore.GetQuery(const ASQL: String): TDataSet;
begin
Result := TADOQuery.Create(Self);
(Result as TADOQuery).CommandTimeout := 0;
(Result as TADOQuery).Connection := Connection;
(Result as TADOQuery).SQL.Text := ASQL;
end;
procedure TCore.SetParamValue(AQuery: TDataSet; const AName: string; const AValue: Variant);
var
i: Integer;
q: TADOQuery;
begin
q := AQuery as TADOQuery;
for i := 0 to q.Parameters.Count - 1 do
if AnsiSameText(AName, q.Parameters[i].Name) then
begin
case VarType(AValue) of
varString, varUString:
q.Parameters[i].DataType := ftString;
varInteger:
q.Parameters[i].DataType := ftInteger;
varInt64:
q.Parameters[i].DataType := ftLargeint;
end;
q.Parameters[i].Value := AValue;
end;
end;
And usage example in your case:
Core.ExecQuery(
'INSERT INTO Stock (StockID, Description, Cost) VALUES (:PStockID, :PDescription, :PCost)',
['PStockID', 'PDescription', 'PCost'],
[4, 'Cheese', 5]);

Stored Procedure insert problems

I have a stored procedure I am calling to insert items in a table. If it only inserts an item into the table I get a 'Operation aborted' exception. If I add a select after the insert, it works fine.
What do I need to do different so I don't get the exception with only the insert?
Delphi code
procedure AddItem(dbCon : TADOConnection; sourcePath : String);
var
addProc : TADOStoredProc;
begin
if FileExists(sourcePath) then
begin
try
addProc := TADOStoredProc.Create(nil);
addProc.Connection := dbCon;
addProc.ProcedureName := 'spTest';
addProc.Open;
finally
addProc.Free();
end;
end;
end;
Stored Procedure
ALTER PROCEDURE [dbo].[spTest]
AS
BEGIN
INSERT INTO dbo.ToSolve (Data, SolveStatus)
VALUES (null, 1)
--SELECT * from dbo.ToSolve --I must have a select or I get and exception
END
As you found out, TADOStoredProc.Open is used in cases where there is a recordset returned.
Use TADOStoredProc.ExecProc when no recordsets are returned. The same goes for TADOQuery, use ExecSQL for INSERT/UPDATE/DELETE statements and Open for SELECT statements. So your example should be like this:
procedure AddItem(dbCon : TADOConnection; sourcePath : String);
var
addProc : TADOStoredProc;
begin
if FileExists(sourcePath) then
begin
addProc := TADOStoredProc.Create(nil);
try
addProc.Connection := dbCon;
addProc.ProcedureName := 'spTest';
addProc.ExecProc;
finally
addProc.Free;
end;
end;
end;

Programmatically finding MS-Access 2007 Table 'Field index' to be present? TField.IsIndexedField not working

My table Customers has a field UserID which is indexed.
Now when I am dropping this Field from delphi, I am getting EOleExecption as its a indexed field.
I tried with following code:
ObjCustomers := TADOTable.Create(nil);
ObjCustomers.Connection := Connection;
ObjCustomers.TableName := 'Customers';
ObjCustomers.Open;
if (ObjCustomers.FindField('UserID').IsIndexField) then
begin
ExecuteSQLStatements(['DROP INDEX UserID ON Customers']);
end;
But this Tfield.IsIndexField is coming up False for this case.
Further I dont wanna do something like this:
try
ExecuteSQLStatements(['DROP INDEX UserID ON Customers']);
except
on E: exception do
end;
Is there any way so that I can check whether the field is Indexed, before executing SQL query?
Thankx in advance!
GetIsIndexField is not implemented by TADODataSet, and the result will be False.
Use TADOConnection.OpenSchema to retrieves table indexes:
var DataSet: TADODataSet;
DataSet := TADODataSet.Create(nil);
try
Connection.OpenSchema(siIndexes, VarArrayOf([Unassigned, Unassigned, Unassigned, Unassigned, 'Customers']), EmptyParam, DataSet);
while not DataSet.Eof do begin
ShowMessage(DataSet.FieldByName('INDEX_NAME').AsString);
DataSet.Next;
end;
finally
DataSet.Free;
end;
To make this answer complete:
As suggested by TLama you can use the TADODataSet method GetIndexNames.
ADO is internally using Command.ActiveConnection.OpenSchema(adSchemaIndexes...
function IsIndexField(DataSet: TADODataSet; FieldName: string): Boolean;
var
SL: TStringList;
begin
SL := TStringList.Create;
try
DataSet.GetIndexNames(SL);
Result := SL.IndexOf(FieldName) <> -1;
finally
SL.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ObjCustomers: TADOTable;
begin
ObjCustomers := TADOTable.Create(nil);
ObjCustomers.Connection := Connection;
ObjCustomers.TableName := 'Customers';
if IsIndexField(TADODataSet(ObjCustomers), 'UserID') then
begin
Showmessage('Index');
Connection.Execute('DROP INDEX UserID ON Customers');
end
else
Showmessage('Not Index');
// ObjCustomers.Open;
ObjCustomers.Free;
end;
VAR
AdoTbl:TAdoDataset;
BEGIN
AdoTbl:=TAdoDataset.Create(Self); // use TAdoDataset
AdoTbl.Connection :=MyAdoConnection;
AdoTbl.CommandType:=cmdTable; //Importent !!
AdoTbl.CommandText:='Refx_Ceramics_Hist_PreHist'; //Tablename
AdoTbl.GetIndexNames(ListBox1.Items);
END;
This works for me on DelphiXE2

Optional Anonymous Method

I would like to expose a function that can take an optional anonymous method :
type
TParamsProc = reference to procedure(Params: TSQLParams);
TFieldsProc = reference to procedure(Fields: TSQLResult);
TDbController = class
...
public
procedure Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
end;
implementation
procedure TDbController.Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
var
Q: TUIBQuery;
begin
Q := TUIBQuery.Create(nil);
try
Q.Database := FDatabase;
Q.Transaction := FTransaction;
Q.SQL.Text := SQL;
ParamsProc(Q.Params);
Q.Open;
while not Q.Eof do
begin
FieldsProc(Q.Result);
Q.Next;
end;
finally
Q.Free;
end;
end;
As sometimes I have no params to pass to a SQL Query, I would like to make the ParamsProc optional.
this code don't work :
if ParamsProc <> nil then ParamsProc(Q.Params);
nor this one :
if #ParamsProc <> nil then ParamsProc(Q.Params);
The first one don't compile, the second one compile but don't work because ParamsProc has always a non nil value.
Example of call :
FController.Select(
'select A, B, C from SOME_TABLE',
nil,
procedure(Fields: TSQLResult)
begin
FA := Fields.AsInteger[0];
FB := Fields.AsString[1];
FC := Fields.AsCurrency[2];
end
);
Edit
Seems that Assigned(ParamsProc) do the trick.
Following Jeroen Pluimers advice, I make my "Edit" an "Answer" :
Assigned(ParamsProc) do the trick :
procedure TDbController.Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
var
Q: TUIBQuery;
begin
Q := TUIBQuery.Create(nil);
try
Q.Database := FDatabase;
Q.Transaction := FTransaction;
Q.SQL.Text := SQL;
if Assigned(ParamsProc) then
ParamsProc(Q.Params);
Q.Open;
while not Q.Eof do
begin
FieldsProc(Q.Result);
Q.Next;
end;
finally
Q.Free;
end;
end;
Hope this helps !

Resources