Mysql installation and C ++ access to the mysql database and encoding settings

Source: Internet
Author: User

1. The installation of mysql is relatively simple. You can download the mysql installation program directly from the official website to complete the installation process. There are many installation tutorials online. There is no need to pay attention to this.

 

II. C ++ accesses mysql. It mainly uses mysql-defined header files and internally defines various data structures and functions, such as MYSQL, MYSQL_RES, MYSQL_ROW, and mysql_real_connect. You must configure the header file, lib file, and dll file to the current development environment to access the mysql database.

Use the latest vs2013 as an example to describe the configuration process. I found several images on the Internet to illustrate the clarity of the writing.

 

1. to specify the header file used by mysql, you can directly copy the header file under the include file under the mysql installation directory to the include directory under the vs installation directory, however, we usually specify an additional header file directory for the compiler. Right-click the project-> properties, and add the Include file of mysql to the Additional include Directory. This file is under the mysql installation Directory, for example, my installation Directory.

C: \ Program Files \ MySQL Server 5.1 \ include



2. Specify the mysql database file

Under the General connector, add the path of the lib folder under the mysql installation Directory to the Additional Liberay Directory:

C: \ Program Files \ MySQL Server 5.5 \ lib

Author: csdn iaccepted lingfeng



3. Add additional dependencies (AdditionalDependencies). For example, to specify libmysql. lib, you must specify which lib file to use in the library file set above.



OK. Now the configuration is complete, and you can connect to mysql and perform database operations.

Author: csdn iaccepted lingfeng


Create a new project in vs2013 and perform database operations based on the mysql official 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] = "person"; char tname [30] = "person "; char * query = nullptr; con = mysql_init (nullptr); if (! Mysql_real_connect (con, dbhost, dbuser, 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 ('20170101', 'pcs')"); mysql_query (con, "select name, id from person where id = '000000'"); results = mysql_store_result (con); cout <mysql_num_fields (results) <endl; while (record = mysql_fetch_row (results) {cout <record [0] <endl ;} mysql_close (con); return 0 ;}

Compile and run it normally.

But sometimes the system prompts

Main. obj: error LNK2019: unresolved externalsymbol _ mysql_num_fields @ 4 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_set_character_set @ 8 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_init @ 4 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_real_connect @ 32 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_query @ 8 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_store_result @ 4 referenced in function _ main

1> main. obj: error LNK2019: unresolved externalsymbol _ mysql_fetch_row @ 4 referenced in function _ main

1> main. obj: error LNK2019: unresolved external symbol_mysql_close @ 4 referenced in function _ main

 

This means that none of these symbols can be found, leading to link failure.

Obviously, the compilation is normal, but the link fails. First, check whether the above three settings are correct. If there is no error, it may be related to the system version, for example, you are using a 64-bit windows system, but the build project is a 32-bit project. This is the same as above. Right-click a project and choose properties> platform at the top of the property pages page. Then, select x64, it will be okay to compile it again..

Author: csdn iaccepted lingfeng

3. Chinese garbled characters

You can see from the mysql user manual that the steps for converting character codes in mysql are clear:

 

1. MySQL Server converts the request data from character_set_client to character_set_connection when receiving the request;

2. Before performing internal operations, convert the request data from character_set_connection to the internal operation character set. The method is as follows:

• SET the character set value for each data field;

• If the preceding value does not exist, use the default character set Value of the corresponding data table (MySQL extension, non-SQL standard );

• If the preceding value does not exist, use the default character set Value of the corresponding database;

• If the preceding value does not exist, use character_set_server to set the value.

3. Convert the operation result from the internal 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 encoding, for example, to gbk, then insert and read the data. At this time, mysql finds that it is gbk encoded when receiving the data. As shown above, it will convert the gbk encoding into character_set_server encoded data and store it, here, gbk is converted to utf8. During reading, mysql automatically converts the read results from the internal character set to the encoding specified by character_set_results, that is, converting from uft8 to gbk will not cause Encoding Problems.

The above is an automatic conversion process. All the programmer has to do is set the internal encoding format and client encoding format. The client here is in a broad sense, the command lineclint can be used only for applications. To put it bluntly, anything you want to access mysql is collectively referred to as a client. For example, in the code in the above example, when the C ++ program acts as a client, use

Mysql_set_character_set (con, "gbk ");

Set the client encoding to gbk. In this way, the Stored Chinese mysql will be converted to utf8 format. During reading, mysql will convert the utf8 format to gbk and return it to the client.

 

View the preceding Code commands

  1. Mysql> show variables like 'character % ';
The values of character_set_server, character_set_connection, and character_set_results are displayed.

 

It is mainly to understand the mysql character encoding and conversion steps mentioned above, so that it can be controlled without garbled characters.

 

Author: csdn iaccepted lingfeng


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.