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

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

Design theory: the basics of font design

<br />Words are the inevitable product of hu...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

How to install Postgres 12 + pgadmin in local Docker (support Apple M1)

Table of contents introduce Support Intel CPU Sup...

How to handle images in Vue forms

question: I have a form in Vue for uploading blog...

Content-type description, that is, the type of HTTP request header

To learn content-type, you must first know what i...

Three Ways to Lock and Unlock User Accounts in Linux

If you already have some kind of password policy ...

Tutorial on installing lamp-php7.0 in Centos7.4 environment

This article describes how to install lamp-php7.0...

Implementation of static website layout in docker container

Server placement It is recommended to use cloud s...

GDB debugging MySQL actual combat source code compilation and installation

Download source code git clone https://github.com...

CSS3 to achieve floating cloud animation

Operation effect html <head> <meta chars...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...