VC + + in the Sqlite3.dll interface to the SQLite database operation, including open the database, insert, query database, etc., if the operation interface input parameters include Chinese characters, will cause an operation exception. For example, calling Sqlite3_open to open the database file, if the file path appears in Chinese, will cause the open failure. SQLITE3_EXEC executes the SQL statement, if it contains Chinese characters, it will become garbled.
This is because the SQLite database uses the UTF-8 encoding, and the passed-in string is ASCII-encoded or Unicode-encoded, resulting in a string format error. The solution is to convert the string to UTF-8 encoding before calling the SQLite interface, which provides a variety of string encoding conversion functions.
Copy Code code as follows:
UTF-8 Turn Unicode
Std::wstring utf82unicode (const std::string& utf8string)
{
int widesize =:: MultiByteToWideChar (Cp_utf8, 0, Utf8string.c_str (),-1, NULL, 0);
if (widesize = = error_no_unicode_translation)
{
Throw Std::exception ("Invalid UTF-8 sequence.");
}
if (widesize = 0)
{
Throw Std::exception ("Error in Conversion.");
}
Std::vector<wchar_t> resultstring (widesize);
int convresult =:: MultiByteToWideChar (Cp_utf8, 0, Utf8string.c_str (),-1, &resultstring[0], widesize);
if (Convresult!= widesize)
{
Throw Std::exception ("La falla!");
}
Return std::wstring (&resultstring[0]);
}
Unicode converted to ASCII
String Widebyte2acsi (wstring& wstrcode)
{
int asciisize =:: WideCharToMultiByte (CP_OEMCP, 0, Wstrcode.c_str (),-1, NULL, 0, NULL, NULL);
if (asciisize = = error_no_unicode_translation)
{
Throw Std::exception ("Invalid UTF-8 sequence.");
}
if (asciisize = 0)
{
Throw Std::exception ("Error in Conversion.");
}
Std::vector<char> resultstring (asciisize);
int Convresult =::widechartomultibyte (CP_OEMCP, 0, Wstrcode.c_str (),-1, &resultstring[0], asciisize, NULL, NULL);
if (Convresult!= asciisize)
{
Throw Std::exception ("La falla!");
}
Return std::string (&resultstring[0]);
}
Utf-8 Turn ASCII
String Utf_82ascii (string& strutf8code)
{
String Strret ("");
Convert UTF8 to Unicode first
Wstring wstr = Utf82unicode (Strutf8code);
Finally convert Unicode to ASCII
strret = WIDEBYTE2ACSI (WSTR);
return strret;
}
///////////////////////////////////////////////////////////////////////
ASCII to Unicode
Wstring acsi2widebyte (string& strascii)
{
int widesize = MultiByteToWideChar (CP_ACP, 0, (char*) Strascii.c_str (),-1, NULL, 0);
if (widesize = = error_no_unicode_translation)
{
Throw Std::exception ("Invalid UTF-8 sequence.");
}
if (widesize = 0)
{
Throw Std::exception ("Error in Conversion.");
}
Std::vector<wchar_t> resultstring (widesize);
int convresult = MultiByteToWideChar (CP_ACP, 0, (char*) Strascii.c_str (),-1, &resultstring[0], widesize);
if (Convresult!= widesize)
{
Throw Std::exception ("La falla!");
}
Return std::wstring (&resultstring[0]);
}
Unicode Turn Utf8
std::string Unicode2utf8 (const std::wstring& widestring)
{
int utf8size =:: WideCharToMultiByte (Cp_utf8, 0, Widestring.c_str (),-1, NULL, 0, NULL, NULL);
if (utf8size = 0)
{
Throw Std::exception ("Error in Conversion.");
}
Std::vector<char> resultstring (utf8size);
int convresult =:: WideCharToMultiByte (Cp_utf8, 0, Widestring.c_str (),-1, &resultstring[0], utf8size, NULL, NULL);
if (Convresult!= utf8size)
{
Throw Std::exception ("La falla!");
}
Return std::string (&resultstring[0]);
}
ASCII Turn Utf8
String Ascii2utf_8 (string& strasciicode)
{
String Strret ("");
Convert ASCII to Unicode first
Wstring wstr = Acsi2widebyte (Strasciicode);
Finally convert Unicode to UTF8
strret = Unicode2utf8 (WSTR);
return strret;
}