Msql does not support inserting Chinese characters. The data set (UNICODE utf8)

Source: Internet
Author: User
Tags intl
Conversion between "Unicode Character Set" and "Multi-Byte !~

Functions are:
See the msdnMS-help: // Ms. msdnqtr. v80.chs/ms. msdn. v80/ms. win32com. v10.en/Intl/unicode_2bj9.htm
Int widechartomultibyte (
Uint
CodePage,// Code page
DWORDDwflags,// Performance and mapping flags
LpcwstrLpwidecharstr,// Wide-character string
IntCchwidechar,// Number of chars in string.
LpstrLpmultibytestr,// Buffer for New String
IntCbmultibyte,// Size of Buffer
LpcstrLpdefaultchar,// Default for unmappable chars
LpboolLpuseddefaultchar// Set when default char used
);
And
See the msdnMS-help: // Ms. msdnqtr. v80.chs/ms. msdn. v80/ms. win32com. v10.en/Intl/unicode_17si.htm
Int multibytetowidechar (
Uint CodePage,
DWORD Dwflags,
Lpcstr Lpmultibytestr,
Int Cbmultibyte,
Lpwstr Lpwidecharstr,
Int Cchwidechar
);

========================================================== ==========================================

The following content is transferred from:Http://www.crazycoder.cn/CDevelopment/Article14240.html

I don't know VC. by default, net2005 uses the "Unicode Character Set" (UNICODE character set). In the past, when using the vc6 project, the default value was "Multi-Byte Character Set. Vc has never been used before. net2005, I always think. net is used for programming on the framework, and there is no need to open the huge program on the MFC. net2005 makes the machine like a rickshaw.

I declare a cstring and assign a value to it as planned, as shown below:

Cstring S;
S. Format ("Count = % d", count );

Based on experience, this certainly won't be wrong, but sorry, the compilation is wrong, because this is the Unicode character set used in my environment, the format function I provided to cstring is "Multi-Byte" (Multi-byte), so the compilation fails. You Need To Know That MessageBox ("DDD") is used in this setting "); compilation fails because the system calls messageboxw, which is the function of the Unicode wide character set.

Fortunately, according to the compiler prompts. format ("Count = % d", count); changed to S. format (_ T ("Count = % d", count);. _ t represents a macro, which means to convert the string to a wide character. Similarly, MessageBox ("DDD"); can be MessageBox (_ T ("DDD "));

However, there is another problem: all the items displayed in the form are wide characters. For example, a is a \ 0 in the memory, and the first byte A is followed by \ 0, when data is removed from the form (such as user input) to interact with other platforms, for example, transmitted over a network to a remote machine. If the Unicode Character Set is not used, a problem occurs. To make the interface consistent with the background transmission, we have to convert the wide character into multiple character sets:

Cstring strwidechar;
Strwidechar. Format (_ T ("this is a byte width "));
Char Buf [20];
Memset (BUF, 0, 20 );

Widechartomultibyte (// convert Unicode to ANSI
Cp_acp,
Wc_compositecheck | wc_defachar char,
Strwidechar,
Strwidechar. getlength (),
(Char *) BUF, // convert to buffer
20, // maximum number of bytes
0,
0
);

Similarly, if you want to display the received string normally on the interface, you must convert it into a wide byte representation (bother ?) :

 

Char chbytes [8];

Memcpy (chbytes, "aaaaaaa \ 0", 8 );
Wchar wch [9];

N = multibytetowidechar (// convert Unicode to ANSI
Cp_acp,
0,
Chbytes,
8,
Wch, // convert to the buffer
8 // maximum number of bytes
);

Wch [N] = '\ 0 ';

In this way, data is retrieved from the interface and displayed to the interface first, but the compiling environment can also be set to multi-byte character set ), you can avoid such conversions (unfortunately, I found that the code was almost finished ). In "project-> Configuration properties-> General-> Character Set, select" use Unicode Character Set "to use the uncode character set, select "use multi-Byte Character Set" as the multi-byte character set.

========================================================== ========================================================== ==================================

Recently, I was working on a project. I used vc6 to write a program to connect to MySQL for data access.

I was planning to use convenient ADO for operations. But after studying it, I found that ADO cannot directly connect to MySQL and must use ODBC. Although ODBC is convenient for programming, it is difficult to use and is said to be of low stability and efficiency. Therefore, we finally decided to use the APIS provided by MySQL.

The APIS provided by MySQL have obvious advantages, high efficiency, and stability. Disadvantages: it is for C itself, not for C ++, so it is not as convenient as writing other MFC programs. Its versatility is poor, and the code portability is almost 0. But the biggest problem is its encoding.

First, this program must be able to display text in both simplified and Traditional Chinese systems, so that the program must use Unicode characters. In addition, the data stored in the simplified and Traditional Chinese systems must be properly stored in the database, and the data can be correctly displayed whether it is read from the simplified, traditional, or even Japanese systems, it is not garbled. MySQL does not support Unicode encoding, and its API cannot even execute SQL statements encoded in Unicode. MySQL supports UTF-8 encoding and cannot be normally displayed in programs written in vc6 ......

After research, we decided to solve the problem in this way. The program uses unicode encoding and the database uses utf8 encoding. Before executing the SQL statement in the program, convert the Unicode-encoded SQL statement to utf8 encoding for execution. The utf8 data retrieved from the database is converted to Unicode and displayed in the program. Two key functions are as follows:

// Convert the UTF-8 encoded string to Unicode and return it as cstring
// For Unicode only
Cstring cts04dlg: utf8tounicode (char * utf8)
{
   DWORD dwunicodelen;    // Unicode length after conversion
   Tchar * pwtext;      // Save the Unicode pointer
   Cstring strunicode;    // Return Value

   // Get the converted length and allocate memory
   Dwunicodelen = multibytetowidechar (cp_utf8, 0, utf8,-1, null, 0 );
   Pwtext = new tchar [dwunicodelen];
   If (! Pwtext)
   {
     Return strunicode;
   }

   // Convert to Unicode
   Multibytetowidechar (cp_utf8, 0, utf8,-1, pwtext, dwunicodelen );

   // Convert to cstring
   Strunicode. Format (_ T ("% s"), pwtext );

   // Clear memory
   Delete [] pwtext;

   // Return the converted Unicode string
   Return strunicode;
}

// Use MySQL APIs to execute Unicode-encoded SQL statements
// The data table is encoded as utf8.
Bool cts04dlg: executeunicodesql (cstring strsql)
{
   DWORD dwutf8len;  // Save the length of the UTF-8 encoded SQL statement
   Char * pstext;    // Save the utf8 encoded SQL pointer

   // Obtain the utf8 encoded SQL length and allocate memory
   Dwutf8len = widechartomultibyte (cp_utf8, null, (lpctstr) strsql,-1, null, 0, null, false );
   Pstext = new char [dwutf8len];
   If (! Pstext)
   {
     Return false;
   }

   // Convert Unicode-encoded SQL statements to utf8 Encoding
   Widechartomultibyte (cp_utf8, null, (lpctstr) strsql,-1, pstext, dwutf8len, null, false );

   // Call MySQL APIs to execute SQL statements
   If (mysql_real_query (& MySQL, pstext, dwutf8len )! = 0)
   {
     Delete [] pstext;
     Return false;
   }

   // Clear memory
   Delete [] pstext;

   // Return that the execution is successful.
   Return true;
}

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.