Prepared Statements
Posted: 02.11.2008, 14:05
Hello boys and girls,
I've been wondering a lot how to use those prepared statements.
I've found TZConnection.DbcConnection.PrepareStatementWithParams, but do I really have to specify the Statement AND the Parameters in one process? I'd like to just specify the prepared statement once, then collect the parameters and last but not least to execute this statement.
However, that's not the main problem though, the point I really would to know is whether the thing I want to do would work with PrepareStatementWithParams.
And what about a TZQuery object? At least it doesn't seem like I'd need it, a TZConnection object seems to be the only required component/object, is this true? Won't I have to open a query for that?
Example code:
Also, how can I specify a binary parameter/a blob? I don't like the thought of putting "binary" data (yes, sure, even text is binary, but that's not the point...) into a TString
s object, really.
I hope you can help me with thoughts, hints and rants regarding my little problem.
Btw, I guess this would also be pretty interesting for other ZeosLib users.
Best regards,
Mihai
I've been wondering a lot how to use those prepared statements.
I've found TZConnection.DbcConnection.PrepareStatementWithParams, but do I really have to specify the Statement AND the Parameters in one process? I'd like to just specify the prepared statement once, then collect the parameters and last but not least to execute this statement.
However, that's not the main problem though, the point I really would to know is whether the thing I want to do would work with PrepareStatementWithParams.
And what about a TZQuery object? At least it doesn't seem like I'd need it, a TZConnection object seems to be the only required component/object, is this true? Won't I have to open a query for that?
Example code:
Code: Select all
var
Statement: IZPreparedStatement;
Params: TStrings;
begin
// Let us assume the connection is set up correctly...
(* We want to execute the following query: *
* INSERT INTO table (a,b,c) VALUES('foo','bar','baz') *
* *
* Basically, this could be done via this prep. st.: *
* INSERT INTO table (a,b,c) VALUES(?,?,?) *)
// Get the three parameters
Params := TStrings.Create;
Params.Add (Dosomefancystuff ());
Params.Add (Justanotherfunction ());
Params.Add (Morefancystuff ());
// Now that we have the parameters, build and execute the statement.
Statement := ZConnection.DbcConnection.PrepareStatementWithParams ('INSERT INTO table (a,b,c) VALUES(?,?,?);', Params);
// OK, but how to execute the statement? I know there is ExecutePrepared and ExecuteQueryPrepared. Due to the fact that I want to use INSERT here, will it just be enough to run...
if (Statement.ExecutePrepared = TRUE) then
// ...
else
// ...
// Or should I use ExecuteQueryPrepared nevertheless?
end;
s object, really.
I hope you can help me with thoughts, hints and rants regarding my little problem.
Btw, I guess this would also be pretty interesting for other ZeosLib users.
Best regards,
Mihai