I will share a solution for MySQL to write Chinese garbled characters, and mysql to write Chinese garbled characters.

Source: Internet
Author: User

I will share a solution for MySQL to write Chinese garbled characters, and mysql to write Chinese garbled characters.
Share a solution for MySQL to write Chinese garbled characters

I have posted a post asking me how to solve the problem of MySQL writing Chinese garbled characters. But no one will, or the other will, don't want to answer. I searched for answers on the Internet and tried to make them invalid many times, so I ran away with a software program for many days due to this garbled problem.

It was not until yesterday that I searched the internet again. After many attempts, I finally solved the garbled problem and completed all the functions of the software.

In addition to joy, I also thought that many people may be troubled by this problem. I understand their feelings. So I decided to share this method.

1. First, after connecting to MySQL, execute the SQL statement (connection handle, "set names 'gbk'") to display Chinese characters when reading the database name, table name, and record data. Of course, you can also execute an SQL statement (connection handle, "set names 'utf8'") and then read the data, convert UTF8 encoding to ANSI encoding (the conversion module and source code are too many and you can find them yourself ). 2. I thought that after the first execution, I could output Chinese characters. It should be okay to enter Chinese characters. However, there is a problem with the input of Chinese characters. The reason is that the database and table created by using the MySQL-supported database creation and table creation commands are as follows, the default character set is the Latin character set (of course, this is what I found here, not all cases ). Therefore, after creating a DATABASE, execute the SQL statement (connection handle, "ALTER DATABASE 'database name' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;") after creating the table, run the SQL statement (connection handle, "ALTER TABLE 'table name' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;") to enter Chinese characters normally. 3. Because single quotes include data when writing a record, the data must be escaped if there are single quotes, and the Condition Statement for deleting the record must also be escaped. Escape handling method: field value = Child text replacement (field value, "\ '", "\ |", true ), "'", "\'", true), "\ |", "\ '", 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.