vc6.0 the garbled problem caused by using the encoding in MySQL database (the encoding of reception and output must be consistent)

Source: Internet
Author: User
Tags server port

"Problems encountered in the compilation"

① when using VC 6.0 to call the data in MySQL, there is a garbled Chinese

② don't understand the code system in MySQL

"Start solving problems"

① opening MySQL Console


② began to show himself



③ Open VC 6.0 (Configure MySQL environment here I will not shiver)

Walk up ..... Copy the following code in VC 6.0 to connect MySQL and the calling code as follows:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
#pragma comment (lib, "Ws2_32")
#pragma comment (lib, "Libmysql")
using namespace Std;

-------------------------------------call the MySQL database--------------------------------------。
void Xian ()
{
Variables like ' char% '
MySQL MySQL;
Mysql_res *resultset;
Mysql_row ROW;
Mysql_init (&mysql);
Mysql_options (&mysql, Mysql_set_charset_name, "UTF8");
cout<< "-------------------------------------------------------" <<endl;
cout<< "| Life | " << "Gender |" << "Age |" << "Diagnostic Results |" << "Prescription |" << "Fees |" << "Time |" <<endl;
cout<< "----------------------------------------------------------------" <<endl;
if (!mysql_real_connect (&mysql, "localhost", "root", "123456", "Zhaofulu", 3306, NULL, 0))
{
cout << "\ n database connection Error!";
}
Else
{
if (mysql_query (&mysql, "SELECT * from users"))
{
cout << "Query failed";
}
Else
{
ResultSet = Mysql_store_result (&mysql);//Get result set
if (mysql_num_rows (resultset) = NULL)
{
int numfields = Mysql_num_fields (resultset);//Get the number of fields in the table

while (row = mysql_fetch_row (resultset))
{
int i = 0;
for (i = 0; i < numfields; i++)
{
cout<< "| "<< row[i]<<" ";
}
cout<< "|";
cout<<endl;
cout<< "----------------------------------------------------------------" <<endl;
}
}
Else
{
cout<< "\ n No query results!";
}
cout<<endl;

}
}

}
---------------------------connection to the database------------------------------。
int main ()
{
const char user[] = "root"; Username
const char pswd[] = "123456"; Password
const char host[] = "localhost"; or "127.0.0.1"
const char table[] = "Zhaofulu"; Database
unsigned int port = 3306; Server port
MySQL MySQL;
Mysql_init (&mysql);
if (Mysql_real_connect (&mysql,host,user,pswd,table,port,null,0))
{
cout<< "Connect succeed!" <<endl;
Xian ();
}
Else
{
cout<< "Connect failed!" <<endl;
}
Mysql_close (&mysql);//Disconnect
return 0;
}

③ running VC 6.0            

"Problem arises"

Running VC 6.0 after the garbled phenomenon:


Garbled phenomenon, not like the pre-input display in MySQL effect (this is a tangle)

At this point you begin to study the code and start looking at the data and get the same results: coding issues

At this point you start to pick out a line of code in the code (the rest of the study understands, the rest of it):

mysql_options (&mysql, Mysql_set_charset_name, "UTF8");//must be defined globally in this function  

You may understand that this is to make all the inputs and outputs in this function be in the format encoded as UTF8

"Start solving problems"

Open the MySQL console (view the encoded properties of the built Zhaofulu database)


The Encoding property of the Zhaofulu database is GBK

② to view all properties of the user table under Zhaofulu


So put the code mysql_options (&mysql, Mysql_set_charset_name, "UTF8");

Change to Mysql_options (&mysql, Mysql_set_charset_name, "latin1");

"Run VC 6.0 Again"


"Another method":

Code modification with command: Set CHARACTER_SET_CLIENT=GBK on the MySQL console

vc6.0 the garbled problem caused by using the encoding in MySQL database (the encoding of reception and output must be consistent)

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.