The difference between storing full-width characters and half-width characters in MySQL

The difference between storing full-width characters and half-width characters in MySQL

Unfortunately, the MYSQL_DATA_TRUNCATED error occurred again during the company's internal testing of IM, and the log record appeared when mysql_stmt_fetch was called. Based on previous experience, it should be caused by the insufficient length of the given result set binding area. I checked it repeatedly many times but did not find the problem. I have corresponding relationships in the code. For example, for char(20), I will define the char buffer[20] array in my code to store it. It looks so right and perfect. I had no choice but to print each line of data, find the line with the error, and discover that the font was different from the others. for example:

123456789 (half-width)
123456789(full width)

This is not controlled by me entering spaces, but by the full-width and half-width input method. For a full-width character, it is twice the length of a half-width character, and my MySQL uses utf-8, so in the database a full-width character is 3 bytes long.

select length(column) from table_name where…;

Use this to print the length and you can tell. At this time, you need to consider the problem of matching the length of the type in the code with the length of the database field, such as full-width characters or Chinese characters. The char(20) in the database represents 20 characters, not 20 bytes. Please note that when retrieving data, do not use 20 as the result set length.

<<:  WeChat Mini Programs Achieve Seamless Scrolling

>>:  Two ways to enable firewall in Linux service

Recommend

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

js to achieve the complete steps of Chinese to Pinyin conversion

I used js to create a package for converting Chin...

Simple writing of MYSQL stored procedures and functions

What is a stored procedure Simply put, it is a se...

CSS -webkit-box-orient: vertical property lost after compilation

1. Cause The requirement is to display two lines,...

A brief discussion on Yahoo's 35 rules for front-end optimization

Abstract: Whether at work or in an interview, opt...

Detailed explanation of javascript knowledge points

Table of contents 1. Basic Introduction to JavaSc...

Jenkins packaging microservices to build Docker images and run them

Table of contents Environment Preparation start 1...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...

Vue implements sample code to disable browser from remembering password function

Find information Some methods found on the Intern...

MySQL database green version installation tutorial to solve system error 1067

What is the difference between the green version ...

Coexistence of python2 and python3 under centos7 system

The first step is to check the version number and...