2017.02.21 Mysql Character Set garbled error process

Source: Internet
Author: User


1, the command line ensures that the following character sets are consistent

* *, Shell's character set

* *, Connect's character set

The character set of the server

*, database, table character set


MySQL API Programming, command line ensures that the following character sets are consistent

* *, code's character set

* *, set names UTF8

*, database, table character set



Here is an example of invoking the MySQL API

[Email protected]/tmp/test$ cat main.c /* *1, shell's character set  *2,  The character set of Connect  *3, server   *4, database, table  character Set  * *1,  Code character set  *2, set names utf8 *3, database, table character set  * */#include  <stdio.h> #include  <stdlib.h> #include  <string.h> #include  <mysql/ Mysql.h> int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) {    int        lRet          = 0;     int       lLoop          = 0;    int       lPos           = 0;    int        lcolumnnum    = 0;    mysql     *hmysqlconnect= null;     MYSQL_ROW ppstRow       = NULL;     MYSQL_RES *result       = NULL;     mysql_field *fields     = null;    char       *pszHost      =  "localhost";     char      *pszUser      =  " Root ";    char      *pszpass       =  "Password"; /*  if it is empty, it is a null character    */    char       *pszDB        =  "MySQL";     char      szbuffer[2048]= {0};    char      * ppszquery[]  = {                             "Set names utf8" ,                             "Drop   database if exists  studentdb ",                             "Create database studentdb  character set utf8 ",                             "Use studentdb" ,                              "Create table student (Id int (2)  primary key auto_ Increment, name varchar (+),  email varchar (+))  default charset=utf8 ",                              "Insert into student values (1,  ' Lao Wang ',  ' [email protected] ') ",                             "insert into  Student values (2,  ' Zhang San ',  ' [email protected] ') ",                              "insert into Student values (3,  ' John Doe ',  ' [email protected] ') ",                              "insert into student values (4,  ' Li Gang ',  ' [email protected] ')",                              "Insert into student (name, email)  values (' Liu Xiang ',  ' [email protected] ') ",                             "select *  From student ",                             "update student set  Email= ' [Email proteCTED] '  where name= ' Liu Xiang ' ",                             "Delete from  student where name= ' John Doe ' ",                             "select *  from student ",                            NULL                              };    /* 1,  Initialize mysql handle   */    hmysqlconnect = mysql_init (NULL)  ;    if  ( Null == hmysqlconneCT)     {        lret =  mysql_errno (hmysqlconnect)  ;        printf ("MySQL initialization failed  \n");         return lRet;    }     printf (  "MySQL initialization succeeded  \n");    /* 2,  link to remote MySQL server   */     /* mysql *mysql_real_connect (Mysql *mysql, const char *host,  const char *user, const char *passwd, const char *db,  Unsigned int port, const char *unix_socket, unsigned long client_flag)  */    hmysqlconnect = mysql_real_connect (HMySQLConnect, pszHost,  pszuser, pszpass, pszdb, 0, null, 0);    if  (NULL  == hmysqlconnect) &NBSP;&NBSp;  {        lret =  mysql_errno ( Hmysqlconnect)  ;        printf (  "MySQL link failed  \n");         return lRet;       }     printf (  "Connect remote MySQL Success  \n");    /* 3,  main loop service  */     while  (1)     {         memset (szbuffer, 0, sizeof (szbuffer));         if (  null == ppszquery[lpos] | | &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0&NBSP;==&NBSP;STRNCMP ( ppszquery[lpos] ,  "Exit"  , 4)  | | &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0&NBSP;==&NBSP;STRNCMP (PpszQuery[lPos]  ,  "EXIT"  , 4)  )         {             break;        }        / * exec sqlquery  */        strncpy (szBuffer,  Ppszquery[lpos++], sizeof (szbuffer));        lret =  mysql_query (Hmysqlconnect, szbuffer);        if  (0 !=  lret)         {             lret =  mysql_errno (Hmysqlconnect)  ;             fprintf ( stderr,  "mysql  query failed  \n");             return lRet;            }                 /* select ?   */        if  (0 &NBSP;==&NBSP;STRNCMP ("select",  szbuffer, 6)  | | &NBSP;0&NBSP;==&NBSP;STRNCMP ("select",  szbuffer, 6))          {            result = mysql_store_result (Hmysqlconnect);             if (NULL ==  Result)             {                 break;             }            / *  output Field name &NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;    lcolumnnum = mysql_field_count (Hmysqlconnect)  ;             fields = mysql_fetch_fields (Result);             memset (Szbuffer, 0, sizeof ( szbuffer));            for  (lLoop =  0; lloop < lcolumnnum; lloop++)              {                 strcat (Szbuffer, fields[lloop].name);                 strcat (szbuffer,  "    ");             }             printf (  "%s&Nbsp;\n ",  szbuffer);            /*  output per line    */            while  ( Ppstrow = mysql_fetch_row (Result))              {                 memset (szbuffer, 0, sizeof (szbuffer));                 for  (lloop = 0; lloop < lcolumnnum;  lloop++)                 {                      strcat (Szbuffer, ppstrow[lloop]);                &Nbsp;    strcat (szbuffer,  "    ");                 }                 printf (  "%s \n",  szbuffer);             }             mysql_free_result (Result);              }    }        /*  Close MySQL  handle */    mysql_close (Hmysqlconnect);     printf (  " Leave mysql \n ");     return lret;} [email protected]/tmp/test$ gcc main.c -l /usr/lib/x86_64-linux-gnu/ - Lmysqlclient  && ./a.out mysql Initialize successfully   connect remote mysql intoGong  id    name    email     1      Lao Wang     [email protected]     2      Zhang San     [email protected]     3     John Doe     [email protected]     4     Li Gang     [email protected]     5     Liu Xiang      [email protected]     id    name     email     1     Lao Wang     [email  protected]     2     Zhang San     [email  protected]     4     Li     [email  Protected]     5     Liu Xiang     [email protected]       Leave Mysql [email protected]/tmp/test$ mysql -hlocalhost  -uroot -p  enter password: welcome to the mysql monitor.  commands end  with ; or \g.your mysql connection id is 19server version:  5.7.17-0ubuntu0.16.04.1  (Ubuntu) copyright  (c)  2000, 2016, oracle and/or  its affiliates. all rights reserved. oracle is a registered trademark of oracle corporation and/or  Itsaffiliates. other names may be trademarks of their respectiveowners . type  ' help; '  or  ' \h '  for help. Type  ' \c '  to clear the current input  statement.mysql> show variables like  ' character% '; +--------------------------+----------------------------+| variable_name             | Value                       |+------ --------------------+----------------------------+| character_set_client     |  utf8                        | |  character_set_connection | utf8                        | |  character_set_database   | latin1                      | |  character_set_filesystem | binary                      | |  character_set_results    | utf8                        | |  character_set_server     | latin1                      | |  character_set_system     | utf8                        | |  character_sets_dir       | /usr/share/mysql/charsets/ |+--- -----------------------+----------------------------+8 rows in set  (0.01 sec) MySQL > select * from studentdb.student;+----+--------+--------------------+| id | name   | email               |+----+--------+---------- ----------+|  1 |  Lao Wang    | [email protected]   | |   2 |  Zhang San    | [email protected] | |   4 |  Li Gang    | [email protected]       | |   5 |  Liu Xiang    | [email protected]  |+----+--------+------- -------------+4 rows in set  (0.00 sec) mysql> ^dbye[email protected]/tmp/ test$


This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1899798

2017.02.21 Mysql Character Set garbled error process

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.