MySQL installation, C + + access to MySQL database, encoding setup issues

Source: Internet
Author: User

A. mysql installation. This is relatively simple, go directly to the official website to download the MySQL installation program. will be able to complete the installation process, the web has a lot of installation tutorials, this is no note.

Second, C + + interview MySQL. The main use of 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 a demonstration sample.

In order to write a clear point, on the Internet to find a few pictures to illustrate.

1. To specify the header file to use for MySQL. Can directly copy the header file under the include file under the MySQL installation folder to the Include folder in the VS installation folder, but generally we will specify an additional header file folder for the compiler. Right-project-> Properties then for example, in this additional include folder (Additional include directory) join on the MySQL include file, this file is in the MySQL installation folder, For example, my installation folder

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



2. Specify the MySQL library file

In the general of the connector below. Additional library folders (Additional Liberay Directory) Join the path to the Lib folder under the MySQL installation folder, my installation folder:

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

This article csdn iaccepted Ling Feng



3. Add additional Dependencies (additionaldependencies), for example by. Specifying Libmysql.lib, in fact, is in the library file set above to specify which Lib file.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvsufjy2vwdgvk/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


OK, here the environment is configured, 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 according to the official MySQL API.

For example, the following:

#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 can be compiled and executed under normal circumstances.

But sometimes it's a hint.

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.

It is very obvious that compiling is normal and simply a link failure. First go back to check whether the above three settings are correct, assuming that there is no error may be related to the system version number, for example, you use a Windows system, but the establishment of project 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 selects x64, It's OK to come back and compile .

This article csdn iaccepted Ling Feng

Three, Chinese garbled problem

Check out the MySQL user manual to find out that the steps for the character encoding conversion of MySQL are very 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, with the following determination methods such as:

? Use the character set value for each data field.

? If the above value does not exist, the default CHARACTER set value of the corresponding data table (MySQL extension) is used. 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 MySQL's default encoding to UTF8 format. Then operate on the client. When there is a Chinese operation. We first change the client's code analogy to GBK, and then insert read, this time when the MySQL received the data is GBK encoded, such as the GBK encoding converted to character_set_server encoded data to deposit. In this case, the GBK is converted to UTF8. At the time of reading, MySQL will voluntarily convert the read results from the internal character set to the Character_set_results specified encoding, that is, from Uft8 to GBK, which does not produce coding problems.

The above conversion to their own initiative process, the program ape to do is just set up the internal encoding format and the client encoding format can be, here the client is generalized. Being able to refer to command Lineclint can also be a mere application, and plainly that whatever is going to visit MySQL is collectively referred to as the client. As the code in the example above. C + + program as a client, because to manipulate the Chinese, so first use

Mysql_set_character_set (con,"GBK");

Set the client encoding to GBK so that the saved Chinese MySQL will be converted to 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% ';


 

is the main step to understand the above mentioned MySQL character encoding conversion, so that you can control the problem of garbled characters.

 

This csdn  iaccepted  lingfeng


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

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.