Represents a pre-compiled SQL statement for later execution.
A PreparedStatement is created by calling Connection::prepareStatement(). The SQL statement may contain in parameters of the form "?". Such parameters represent unspecified literal values (or "wildcards") to be filled in later by the bind methods defined in this class. Each in parameter has an associated index number which is its sequence in the statement. The first in '?' parameter has index 1, the next has index 2 and so on.
Consider this statement:
There are two in parameters in this statement, the parameter for setting the name has index 1 and the one for the photo has index 2. To set the values for the in parameters we use bindValues() with two values, one for each in parameter. Or we can use bind() to set the parameter values one by one.
The following examples demonstrate how to create and use a PreparedStatement.
This example shows how to prepare a statement, bind multiple values at once, and execute it:
Instead of binding all values at once, we can also bind values one by one by specifying the parameter index we want to set a value for:
A PreparedStatement can be reused. That is, the method execute() can be called one or more times to execute the same statement. Clients can also set new in parameter values and re-execute the statement as shown in this example, where we also show use of a transaction and exception handling:
bindValues() or bind() can be used to set a Unix timestamp value as a time_t
type. To set Date, Time or DateTime values, simply use one of the bind methods to set a time string in a format understood by your database. For instance to set a SQL Date value,
See Connection::executeQuery()
Prepared Statement is particularly useful when dealing with user-submitted data, as properly used Prepared Statements provide strong protection against SQL injection attacks. By separating SQL logic from data, PreparedStatements ensure that user input is treated as data only, not as part of the SQL command.
A PreparedStatement is reentrant, but not thread-safe and should only be used by one thread (at a time).
Public Member Functions | |
Parameters | |
template<typename T > | |
void | bind (int parameterIndex, T &&x) |
Binds a value to the prepared statement. | |
template<typename... Args> | |
void | bindValues (Args &&... args) |
Binds multiple values to the Prepared Statement at once. | |
Functions | |
void | execute () |
Executes the prepared SQL statement. | |
long long | rowsChanged () noexcept |
Gets the number of rows affected by the most recent SQL statement. | |
Properties | |
int | getParameterCount () noexcept |
Gets the number of parameters in the prepared statement. | |
Binds a value to the prepared statement.
This method can bind different types of values:
T | The type of the value to bind |
parameterIndex | The index of the parameter to bind (1-based) |
x | The value to bind |
sql_exception | If a database access error occurs or parameterIndex is invalid |
void bindValues | ( | Args &&... | args | ) |
Binds multiple values to the Prepared Statement at once.
args | Values to bind to the Prepared Statement. |
sql_exception | If a database error occurs or if argument count is incorrect |
void execute | ( | ) |
Executes the prepared SQL statement.
sql_exception | If a database access error occurs |
|
nodiscardnoexcept |
Gets the number of rows affected by the most recent SQL statement.
If used with a transaction, this method should be called before commit is executed, otherwise 0 is returned.
|
nodiscardnoexcept |
Gets the number of parameters in the prepared statement.
Copyright © Tildeslash Ltd. All rights reserved.