Represents a connection to a SQL database system.
Use a Connection to execute SQL statements. There are three ways to execute statements: execute() is used to execute SQL statements that do not return a result set. Such statements are INSERT, UPDATE or DELETE. executeQuery() is used to execute a SQL SELECT statement and return a result set. These methods can handle various data types, including binary data, by automatically creating a PreparedStatement when arguments are provided. For more complex scenarios or when reusing statements, you can explicitly create a PreparedStatement object using the prepareStatement() method.
The method executeQuery() will return an empty ResultSet (not null) if the SQL statement did not return any values. A ResultSet is valid until the next call to Connection execute or until the Connection is returned to the ConnectionPool. If an error occurs during execution, an sql_exception is thrown.
Any SQL statement that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed at the conclusion of the command.
Transactions can also be started manually using beginTransaction(). Such transactions usually persist until the next call to commit() or rollback(). A transaction will also rollback if the database is closed or if an error occurs. Nested transactions are not allowed.
Examples
Basic Query Execution
std::cout <<
"Name: " << result.
getString(
"name").value_or(
"N/A")
<<
", Age: " << result.
getInt(
"age") << std::endl;
}
Represents a connection to a SQL database system.
Definition zdbpp.h:1333
ResultSet executeQuery(const std::string &sql, Args &&... args)
Executes a SQL query and returns a ResultSet.
Definition zdbpp.h:1563
Represents a database result set.
Definition zdbpp.h:590
int getInt(int columnIndex)
Gets the designated column's value as an int.
Definition zdbpp.h:731
bool next()
Moves the cursor to the next row.
Definition zdbpp.h:677
std::optional< std::string_view > getString(int columnIndex)
Gets the designated column's value as a string.
Definition zdbpp.h:707
Transaction Example
try {
con.
execute(
"UPDATE accounts SET balance = balance - ? WHERE id = ?", 100.0, 1);
con.
execute(
"UPDATE accounts SET balance = balance + ? WHERE id = ?", 100.0, 2);
std::cout << "Transfer successful" << std::endl;
std::cerr << "Transfer failed: " << e.what() << std::endl;
}
void execute(const std::string &sql, Args &&... args)
Executes a SQL statement, with or without parameters.
Definition zdbpp.h:1525
void commit()
Commits the current transaction.
Definition zdbpp.h:1470
void beginTransaction(TRANSACTION_TYPE type=TRANSACTION_DEFAULT)
Begins a new transaction with optional isolation level.
Definition zdbpp.h:1454
Exception class for SQL related errors.
Definition zdbpp.h:275
Using PreparedStatement
auto stmt = con.
prepareStatement(
"INSERT INTO logs (message, timestamp) VALUES (?, ?)");
stmt.
bindValues(
"User logged in", std::time(
nullptr));
stmt.execute();
std::cout << "Rows affected: " << stmt.rowsChanged() << std::endl;
PreparedStatement prepareStatement(const std::string &sql)
Prepares a SQL statement for execution.
Definition zdbpp.h:1596
void bindValues(Args &&... args)
Binds multiple values to the Prepared Statement at once.
Definition zdbpp.h:1161
A Connection is reentrant, but not thread-safe and should only be used by one thread at a time.
- Note
- When a Connection object goes out of scope, it is automatically returned to the pool. If the connection is still in a transaction at this point, the transaction will be automatically rolled back, ensuring data integrity even in the face of exceptions.
- Warning
- Connection objects are internally managed by the ConnectionPool that created them and are not copyable or movable. Always ensure that the originating ConnectionPool object remains valid for the entire duration of the Connection's use. It is recommended to obtain a Connection, use it for a specific task, and then close it (return it to the pool) as soon as possible, rather than holding onto it for extended periods.