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

Mini Program implements list countdown function

This article example shares the specific code for...

Ubuntu starts the SSH service remote login operation

ssh-secure shell, provides secure remote login. W...

HTML Basics - Simple Example of Setting Hyperlink Style

*** Example of setting the style of a hyperlink a...

Detailed explanation of Docker Swarm service orchestration commands

1. Introduction Docker has an orchestration tool ...

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

Vue component library ElementUI realizes the paging effect of table list

ElementUI implements the table list paging effect...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

Native JS to achieve directory scrolling effects

Here is a text scrolling effect implemented with ...

HTML tag full name and function introduction

Alphabetical DTD: Indicates in which XHTML 1.0 DT...

How to solve the phantom read problem in MySQL

Table of contents Preface 1. What is phantom read...

Vue Element-ui implements tree control node adding icon detailed explanation

Table of contents 1. Rendering 2. Bind data and a...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

How to use MySQL covering index and table return

Two major categories of indexes Storage engine us...