Solve Chinese garbled characters in the SQLite Database

Source: Internet
Author: User
When using the SQLite database, we may find that Chinese characters are displayed when inserting data into the database, however, garbled characters are displayed when read through SQLite. This is because the encoding method supported by SQLite is different from the encoding method in our program. The SQLite database uses UT.

When using the SQLite database, we may find that Chinese characters are displayed when inserting data into the database, however, garbled characters are displayed when read through SQLite. This is because the encoding method supported by SQLite is different from the encoding method in our program. The SQLite database uses UT.

Analysis and solutions for Chinese garbled characters in SQLite

When using the SQLite database, we may find that Chinese characters are displayed when inserting data into the database, but garbled characters are displayed when reading data through SQLite, this is because the encoding method supported by the SQLite database is different from the encoding method in our program, the SQLite database uses the UTF-8 encoding method, we often use the wide-byte uncoid encoding method in the program, so garbled characters will be displayed after the SQLite database is read, because the encoding method is different. For example, for a dialog box-based program, we need to display the data read in our database on the listctrl control. When we write a program for reading, the list control displays unreadable garbled characters, this is the reason.

For example, you can use the sqlite3.dll interface in VC ++ 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 may 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.

There is a problem is definitely to solve the problem, I beginner SQLite, by looking for information, this is because the sqlite database uses the UTF-8 encoding method, and the input string is ASCII or Unicode encoding, the string format is incorrect. The solution is to convert a string to UTF-8 encoding before calling the sqlite interface, which provides various string encoding conversion functions .. The function is as follows:

In general we use vc ++ programming, the most used function is the UTF-8 to Unicode, the function is for reference only

// 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
 
  
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
 
  
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]);}
 
 
 
 
// Convert UTF-8 to ascii string UTF_82ASCII (string & strUtf8Code) {string strRet (""); // convert utf8 to unicode 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
 
  
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
 
  
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 wstring wstr = Acsi2WideByte (strAsciiCode ); // convert unicode to utf8 strRet = Unicode2Utf8 (wstr); return strRet ;}

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.