Handle zero bytes in strings and binary data
masterBecause libpqxx wraps the C-level libpq library, most strings passed to the library must be C-compatible. This means they must end with a single null (0) byte and cannot contain internal null bytes.
The risk with text strings
If you pass a string containing a zero byte as a parameter (e.g., a prepared statement name or a parameter value), the library will treat the string as ending at the first zero byte encountered. The resulting value will be truncated to everything before that byte.
Handling binary data
If you need to pass data containing zero bytes, you must treat it as binary data (SQL BYTEA type) rather than a text string. In libpqxx, represent binary data using contiguous memory containing std::byte. Supported types include:
pqxx::bytespqxx::bytes_viewstd::vector<std::byte>std::array<std::byte, ...>std::span<std::byte>