Solutions to Chinese problems in two MySQL stored procedures

Source: Internet
Author: User

MySQL databaseThe following problems often occur in stored procedures:

1. garbled storage information, especially by executing SQL scripts to add data.

2. The where clause is used to compare Chinese strings, which is also very common.

To solve the problem of garbled storage information, be sure to pay attention to the terminal that executes the script. The default character encoding of the system is what you require. This problem is ultimately caused by mysql.Character Set. MySQL Character Set Support includes Characterset and Collation ).

The support for character sets is refined to four levels: server, database, table, and connection ).

MySQL can refine the character set designation to a database, a table, and a column.

Character Set-related commands:

View the default character set (mysql uses latin1 (ISO_8859_1) by default)

 
 
  1. mysql> SHOW VARIABLES LIKE 'character%';    
  2.  
  3. mysql> SHOW VARIABLES LIKE 'collation_%';   

Run the following command to modify the character set:

 
 
  1. <pre name="code" class="html">     
  2.  
  3. mysql> SET character_set_client = utf8 ;    
  4.  
  5. mysql> SET character_set_connection = utf8 ;    
  6.  
  7. mysql> SET character_set_database = utf8 ;    
  8.  
  9. mysql> SET character_set_results = utf8 ;    
  10.  
  11. mysql> SET character_set_server = utf8 ;    
  12.  
  13. mysql> SET collation_connection = utf8 ;    
  14.  
  15. mysql> SET collation_database = utf8 ;    
  16.  
  17. mysql> SET collation_server = utf8 ;   

The cause of Issue 1 is that the default character set for the table is set to utf8 and the query is sent by UTF-8 encoding, but the connection layer encoding is still incorrect. The solution is to execute the following statement before sending the query:

 
 
  1. SET NAMES 'utf8'; 

It is equivalent to the following three commands:

 
 
  1. <pre name="code" class="html">SET character_set_client = utf8;    
  2.  
  3. SET character_set_results = utf8;    
  4.  
  5. SET character_set_connection = utf8;   

Problem 2 solution:

Transcode COLLATE utf8_unicode_ci for variables or constants to be compared, for example:

 
 
  1. declare cur_preferences cursor for select id from preferences where @name like concat("%",rTitle COLLATE utf8_unicode_ci )  ; 

If the preceding statement is not optimized successfully, run the following statement:

 
 
  1. declare cur_preferences cursor for select id from preferences where @name  COLLATE utf8_unicode_ci  like concat("%",rTitle COLLATE utf8_unicode_ci )  ; 

This will solve this problem.

For more information about MySQL database stored procedures

Related Article

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.