Cause Analysis and Solution of sqlite Chinese Garbled text

Source: Internet
Author: User

In VC ++, you can use the sqlite3.dll interface to operate the sqlite database, including opening the database, inserting and querying the database. If the input parameters of the operation interface contain Chinese characters, the operation will be abnormal. For example, you can call sqlite3_open to open a database file. If the file path contains Chinese characters, opening may fail. Sqlite3_exec: Execute the SQL statement. If it contains Chinese characters, it will become garbled.

This is because the sqlite database uses the UTF-8 encoding method, and the input string is ASCII or Unicode encoding, resulting in a string format error. The solution is to convert a string to UTF-8 encoding before calling the sqlite interface, which provides various string encoding conversion functions.Copy codeThe Code is as follows: // UTF-8 to 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]);
}
// Convert unicode 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 to ascii
String UTF_82ASCII (string & strUtf8Code)
{
String strRet ("");
// Convert utf8 to unicode first
Wstring wstr = Utf82Unicode (strUtf8Code );
// Convert unicode to ascii
StrRet = WideByte2Acsi (wstr );
Return strRet;
}
//////////////////////////////////////// ///////////////////////////////
// Convert 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 to 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]);
}
// Convert ascii to Utf8
String ASCII2UTF_8 (string & strAsciiCode)
{
String strRet ("");
// Convert ascii to unicode first
Wstring wstr = Acsi2WideByte (strAsciiCode );
// Convert unicode to utf8
StrRet = Unicode2Utf8 (wstr );
Return strRet;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.