I’m working in C++ and I’m dealing with an SQLite database. Consider this:
wchar_t s[] = L"This is my wide string. ĂÎȘȚÂăîșțâ";
What should I do to launch a query containing this string? I saw that if I declare my query, for example:
char q[] = "SELECT * FROM 'mydb' WHERE 'something' LIKE '%ĂÎȘȚÂ%'";
The compiler is smart enough to somehow convert those wide chars and the query actually works as expected. But still, how should I declare/create the query array of chars so as to include those pesky characters? I tried with string and its c_str() method, wstring, etc.
You aren’t composing your SQL with string concatenation, aren’t you? Use placeholders and bind the string to the right placeholder:
However, this code will work only on systems that use UTF-16 for wide strings, which is basically only Windows. It’s recommended to not use
wchar_tbecause it’s not portable. Use narrow char UTF-8 encoded strings instead. SQLite assumes all narrowcharstrings are UTF-8, even for file names, even on Windows. Using this approach, you can get the original code to work just by saving the file as UTF-8 without BOM. This will make the stringUTF-8 encoded on major compilers (including MSVC and GCC).