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)