Merge pull request #404 Add documentation for prepared statements in transactions from ewarchul/query_transactions_example

This commit is contained in:
Sébastien Rombauts 2023-02-02 07:45:38 +01:00 committed by GitHub
commit ad6606b320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -373,6 +373,42 @@ catch (std::exception& e)
}
```
### The third sample shows how to manage a prepared statement with a transaction:
```C++
try
{
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
db.exec("DROP TABLE IF EXISTS test");
db.exec("CREATE TABLE test (value INTEGER)");
// Begin transaction
SQLite::Transaction transaction(db);
// Prepare query
SQLite::Statement query {db, "INSERT INTO test (value) VALUES (?)"};
// Collection to save in database
std::vector<int> values{1, 2, 3};
for (const auto& v: values)
{
query.bind(1, v);
query.exec();
query.reset();
}
// Commit transaction
transaction.commit();
}
catch (std::exception& e)
{
std::cout << "exception: " << e.what() << std::endl;
}
```
### How to handle assertion in SQLiteC++:
Exceptions shall not be used in destructors, so SQLiteC++ uses SQLITECPP_ASSERT() to check for errors in destructors.
If you don't want assert() to be called, you have to enable and define an assert handler as shown below,