Ubuntu 12.04 LTS, MySQL does not support Chinese characters, and the Chinese characters read from the database by C language are displayed ???

Source: Internet
Author: User

Disclaimer: This article applies to reading data stored in MySQL using C language in Linux (maybe only for Ubuntu ??? .

It is not to say that a simple "MySQL does not support Chinese" (that is, it only uses MySQL itself and does not need to leave the MySQL terminal from beginning to end (mysql> ), for example, it is not used to read from the database in programming languages ). In addition, this so-called unsupported Chinese is easy to solve. The simplest method I know Is that varchar is not needed.
However, using nvarchar can insert output Chinese characters.

MySQL does not support Chinese characters, which is not a rare problem. After I encountered this problem, I quickly went online to check the problem. As a result, numerous technical blogs told me:

Start with root .... My. CNF file, and then modify the settings, for example, in [MySQL], in [mysqld], in [mysql-safe, the content in [client] or [server] is basically nothing more than adding set_default_charcter = utf8. Then restart the MySQL service. I tried this one and the result tells me that what else I read in the terminal using C language is still a question mark. 1. Otherwise, I will no longer be able to start the MySQL service.
What ...... Sock errors.

========================================================== ========================================================== ===

The above is the background to avoid misleading others.

To solve this problem, you need to add a line of code to solve it.

The following is my instance code:

#include<stdio.h>#include<mysql/mysql.h>#include<assert.h>int main(){MYSQL* mysql;mysql=mysql_init(NULL);mysql=mysql_real_connect(mysql,"localhost","root","123","mydatabase",0,NULL,0);
 
// What you need is the int utf8 below this line; utf8 = mysql_query (MySQL, "set character_set_results = utf8"); printf ("utf8 = % d \ n ", utf8); Assert (utf8 = 0 ); // In fact, what you need is the above part of this line, and then you will have everything else ^_^ the above four lines of code and the following code are almost meaningless, because this is not a problem
MYSQL_RES* result;MYSQL_ROW row;int ret;ret=mysql_query(mysql,"select * from zhaos");assert(ret==0);printf("select finished\n");result=mysql_store_result(mysql);int num;num=mysql_num_rows(result);printf("ROWS= %d\n",num);while((row=mysql_fetch_row(result))!=NULL){    printf("%s\n",row[0]?row[0]:"NULL");mysql_free_result(result);mysql_close(mysql);return 0;}}



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.