MySQL installation, C + + Access MySQL database, encoding setup issues

Source: Internet
Author: User
Tags set set

MySQL installation, this relatively simple, go directly to the official website to download the MySQL installation program, you can complete the installation process, there are many installation tutorials on the Web, this is nothing to note.

Second, C + + access to MySQL, mainly using the MySQL definition of the header file, the internal definition of various data structures and functions, such as mysql,mysql_res,mysql_row,mysql_real_connect and so on a series of structures and functions. It is important to note that the header files and Lib files and DLL files are configured into the current development environment to access the MySQL database.

Describe the configuration process with the latest vs2013 as an example. In order to write a clear point, on the Internet to find a few pictures to illustrate.

1. To specify the header file used by MySQL, you can directly copy the header file under the include file in the MySQL installation directory to the Include directory of the VS installation directory, but generally we will specify an additional header file directory for the compiler. Right-click Project and properties then, as in this additional include directory (Additional includes directory), add the include file on MySQL, this file is in the MySQL installation directory, such as my installation directory

C:\Program Files\mysql\mysql Server 5.1\include



2. Specify the MySQL library file

Under General connectors, additional library directories (Additional Liberay directory) Add the path to the Lib folder on the MySQL installation directory, my installation directory:

C:\Program Files\mysql\mysql Server 5.5\lib

This article csdn iaccepted Ling Feng



3. Add additional Dependencies (additionaldependencies), such as specifying Libmysql.lib, is actually in the library file set above to specify which Lib file.



OK, here the environment is configured to complete, then you can connect MySQL and database operations.

This article csdn iaccepted Ling Feng


Create a new project in vs2013 and then complete the database operation based on the official MySQL API.

As follows:

#include "person.h" #include <Windows.h> #include <iostream> #include <string> #include <mysql.h > #include <winsock.h>using namespace std;int main () {MYSQL *con; Mysql_res *results; Mysql_row Record;char dbuser[30] = "root"; char dbpasswd[30] = "123456"; char dbhost[30] = "localhost"; char dbname[30] = "PE Rson "; char tname[30] =" person "; char *query = Nullptr;con = Mysql_init (nullptr); if (!mysql_real_connect (Con, dbhost, Dbus Er, dbpasswd, dbname, 3306, NULL, 0)) {cerr << "Failed to connect Database" << endl;exit (2);} Mysql_set_character_set (Con, "GBK"); mysql_query (Con, "INSERT into person (ID, name) VALUES (' 370983198811256977 ', ')" ); mysql_query (Con, "Select Name,id from the person where id = ' 370983198811256977 '"); results = Mysql_store_result (con); cout & lt;< Mysql_num_fields (results) << endl;while (record = mysql_fetch_row (results))) {cout << record[0] << Endl;} Mysql_close (con); return 0; }

It is OK to compile and run under normal circumstances.

But sometimes it prompts

Main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved Externalsymbol [email protected] referenced in function _main

1>main.obj:error lnk2019:unresolved External [email protected] referenced in function _main

This means that none of these symbols can be found, causing the link to fail.

Obviously compile is normal only link failure, first go back to check the above three settings is correct, if there is no error, it may be related to the system version, for example, you use a Windows system, However, the establishment of the project is a 32-bit project. This is the same as above, the project (project) Right-click option (properties) has a platform selection (platform) on the top of the property pages, and then chooses x64 so that it is OK to come back to compile .

This article csdn iaccepted Ling Feng

Three, Chinese garbled problem

Looking at the MySQL user manual, you can see that the steps for the character encoding conversion of MySQL are clear:

1. When MySQL server receives the request, it converts the request data from character_set_client to character_set_connection;

2. Convert the request data from character_set_connection to the internal operation character set before doing an internal operation, and determine the following method:

? Use the character set set value for each data field;

? If the above value does not exist, the default CHARACTER set value of the corresponding data table is used (MySQL extension, non-SQL standard);

? If the above value does not exist, the default CHARACTER set value of the corresponding database is used;

? If the above value does not exist, use Character_set_server to set the value.

3. Convert the operation result from the internal operation character set to Character_set_results.

In general, we set the default MySQL encoding to UTF8 format, and then operate on the client, when there is a Chinese operation, we first change the client's code, such as to GBK, and then insert Read, when MySQL found that the data is GBK encoded, This translates the GBK encoding into character_set_server encoded data, where the GBK is converted to UTF8. When read, MySQL automatically converts the read result from the internal character set to the encoding specified by Character_set_results, that is, from Uft8 to GBK, which does not produce a coding problem.

The above conversion to the automatic process, the programmer to do is just set up the internal encoding format and client-side encoding format can be, here the client is generalized, can either refer to the command lineclint can also only the application, plainly speaking is anything to access the MySQL things collectively referred to as the client, As the code in the above example, C + + program as a client, because to manipulate Chinese, so first use

Mysql_set_character_set (con,"GBK");

Set the client code to GBK, so that the stored Chinese MySQL will be converted into the UTF8 format, when read MySQL will be converted from UTF8 format to GBK return to the client.

View commands for each encoding above

  1. MySQL > SHOW VARIABLES like ' character% ';
you can see the values of Character_set_server, Character_set_connection, Character_set_results, and so on.

The main point is to understand the above-mentioned MySQL character encoding conversion steps, so that you can control the problem is not garbled.

This article csdn iaccepted Ling Feng


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.