According to doc. sqlite3_column_text() is declared as:
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
For any reason, it returns a const unsigned char*
. (This might be for historical reasons to emphasize the fact that the returned string is UTF-8 encoded.)
Thus, for assignment to a std::string
(which can be assigned with const char*
expressions among others), a small dirty trick does the job:
std::string myResult = (const char*)sqlite3_column_text(dbResult, 1);
This reclaims the sequence of unsigned char
s to be a sequence of char
s.
Please, note that the signedness of char
is left to the compiler implementation and may be signed or unsigned. (In the major compilers MSVC, g++, clang, it's in fact signed.) Hence, it's accompanied by types signed char
and unsigned char
to make the signedness explicit (and independent of the used compiler) when necessary. The conversion in the above snippet doesn't change any contents of the returned string — it just makes it compatible for the assignment to std::string
.
Googling a bit, I found another Q/A where the answer explains that the "small dirty trick" is legal according to the C++ standard:
Can I turn unsigned char into char and vice versa?