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;}}