Name

sqlite3_snprintf() — Format a string

Definition

char* sqlite3_snprintf( int bytes, char* buf, const char* format, ... );
bytes

The number of bytes available in the output buffer.

buf

A pre-allocated buffer to receive the formatted string.

format

The format string used to build the output string. This is similar to the standard printf() style formatted string, but it supports a few extra formatting flags.

Additional parameters

Message formatting parameters.

Returns

A pointer to the formatted string buffer.

Description

This function formats and builds a UTF-8 string in the provided buffer. It is designed to mimic the standard snprintf() function. Assuming the provided buffer is one byte or larger, the string will always be null-terminated.

Note that the first two parameters of sqlite3_snprintf() are reversed from the standard snprintf(). Also, snprintf() returns the number of characters in the output string, while sqlite3_snprintf() returns a pointer to the buffer passed in by the application.

In addition to the standard %s, %c, %d, %i, %o, %u, %x, %X, %f, %e, %E, %g, %G, and %% formatting flags, all SQLite printf() style functions also support the %q, %Q, %z, %w, and %p flags.

The %q flag is similar to %s, only it will sanitize the string for use as an SQL string literal. Mostly, this consists of doubling all the single quote characters (') to form a proper SQL escape (''). Thus, %q will take the input string O'Reilly and output O''Reilly. The formatted string should contain enclosing ...

Get Using SQLite now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.