| 1 |
/*! \page escaping Escaping Strings |
| 2 |
|
| 3 |
\note Before you read this, remember escaping strings directly and including them in SQL statements is \a bad idea, you should |
| 4 |
use \ref stat "prepared statements" instead. However if you really know what you are doing, continue reading. |
| 5 |
|
| 6 |
|
| 7 |
You can escape strings from unknown source using session's \ref cppdb::session::escape() "escape()" functions. Also |
| 8 |
note that they do not add first and last quotation marks and you are expected to do this on your own. |
| 9 |
|
| 10 |
For example: |
| 11 |
|
| 12 |
\code |
| 13 |
std::string safe_data = sql.escape(data); |
| 14 |
sql << "INSERT INTO names(name) values('" + safe_data + "')" << cppdb::exec; |
| 15 |
\endcode |
| 16 |
|
| 17 |
Please notice the quotes inserted in the query. |
| 18 |
|
| 19 |
But still it is better to do following: |
| 20 |
|
| 21 |
\code |
| 22 |
sql << "INSERT INTO names(name) values(?)" << data << cppdb::exec; |
| 23 |
\endcode |
| 24 |
|
| 25 |
\note \ref odbc "ODBC" backend does not support escaping strings and would throw \ref cppdb::not_supported_by_backend "not_supported_by_backend" exception. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
|