Use MySQL in C ++
The software on my computer is as follows:
VS2012
For MySQL 5.6
The operating system is 64-bit win8.1
1. Prerequisites
Download and install MySQL
2. Configuration
(1) Add the include directory: MY_ SQL directory/include to project properties C ++ general.
(2) add an additional library Directory: MY_ SQL directory/lib
(3) add an additional dependency in "properties" connector "input": libmysql. lib
3. Notes
If the above configuration is followed but the compilation fails, the following message is displayed:
1> generating code...
1> BaseDB. obj: error LNK2019: external symbol that cannot be parsed _ mysql_init @ 4, which is in the function "public: bool _ thiscall BaseDB: openConnect (void )"(? In openConnect @ BaseDB @ QAE_NXZ), BaseDB is referenced. obj: error LNK2019: external symbol that cannot be parsed _ mysql_real_connect @ 32, which is in the function "public: bool _ thiscall BaseDB: openConnect (void )"(? In openConnect @ BaseDB @ QAE_NXZ), BaseDB is referenced. obj: error LNK2019: external symbol that cannot be parsed _ mysql_query @ 8, which is in the function "public: struct st_mysql_res * _ thiscall BaseDB: doQuery (class std :: basic_string <char, struct std: char_traits <char>, class std: allocator <char> const &)"(? DoQuery @ BaseDB @ QAEPAUst_mysql_res @ ABV? $ Basic_string @ DU? $ Char_traits @ D @ std @ V? $ Allocator @ D @ 2 @ std @ Z) is referenced in 1> BaseDB. obj: error LNK2019: external symbol that cannot be parsed _ mysql_store_result @ 4, which is in the function "public: struct st_mysql_res * _ thiscall BaseDB: doQuery (class std :: basic_string <char, struct std: char_traits <char>, class std: allocator <char> const &)"(? DoQuery @ BaseDB @ QAEPAUst_mysql_res @ ABV? $ Basic_string @ DU? $ Char_traits @ D @ std @ V? $ Allocator @ D @ 2 @ std @ Z) is referenced in 1> BaseDB. obj: error LNK2019: external symbol that cannot be parsed _ mysql_close @ 4, which is in the function "public: void _ thiscall BaseDB: closeConnect (void )"(? CloseConnect @ BaseDB @ QAEXXZ) is referenced in 1> DBUtil. obj: error LNK2019: external symbol that cannot be parsed _ mysql_free_result @ 4, which is in the function "public: static bool _ cdecl DBUtil: checkUserLogin (class std: basic_string <char, struct std: char_traits <char>, class std: allocator <char> const &, class std: basic_string <char, struct std: char_traits <char>, class std: allocator <char> const &)"(? CheckUserLogin @ DBUtil @ SA_NABV? $ Basic_string @ DU? $ Char_traits @ D @ std @ V? $ Allocator @ D @ 2 @ std @ 0 @ Z) referenced 1> E:/Flighting/MySQLtest/ConsoleApplication1/Debug/ConsoleApplication1.exe: fatal error nkl1120: six External commands that cannot be parsed 1>
1> generation failed.
The cause of this problem is: MySQL64-bit is installed, while VS2012's default running platform is 32-bit, so it is not supported.
There are two solutions: (1) There is a Configuration Manager in the upper-right corner of project properties. Change the running platform of your project to X64 (if not, create a new one)
(2) If other 32-bit additional libraries have been introduced in the project, rashly changing to 64-bit causes the original library compilation to fail, in this way, you cannot search for a lib and dll named mysql32 on the Internet.
Finally, copy libmysql and dll to System32/SysWow64 if you cannot find libmysql. dll.
Code example:
# Include <Windows. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <mysql. h ># include <iostream> using namespace std; int main () {const char user [] = "root"; // username const char pswd [] = "root "; // password const char host [] = "localhost"; // or "127.0.0.1" const char table [] = "test"; // database unsigned int port = 3306; // server port MYSQL myCont; MYSQL_RES * result; MYSQL_ROW SQL _r Ow; MYSQL_FIELD * fd; char column [32] [32]; int res; mysql_init (& myCont); if (mysql_real_connect (& myCont, host, user, pswd, table, port, NULL, 0) {cout <"connect succeed! "<Endl; mysql_query (& myCont," set names gbk "); // sets the encoding format. Otherwise, the Chinese res = mysql_query (& myCont, "select * from samples"); // query if (! Res) {result = mysql_store_result (& myCont); // Save the queried data to result if (result) {int I, j; cout <"number of result: "<(unsigned long) mysql_num_rows (result) <endl; for (I = 0; fd = mysql_fetch_field (result); I ++) // obtain the column name {strcpy (column [I], fd-> name);} j = mysql_num_fields (result); for (I = 0; I <j; I ++) {printf ("% s \ t", column [I]);} printf ("\ n"); while (SQL _row = mysql_fetch_row (result )) // obtain the specific data {for (I = 0; I <j; I ++) {printf (" % S \ n ", SQL _row [I]);} printf (" \ n ") ;}} else {cout <" query SQL failed! "<Endl ;}}else {cout <" connect failed! "<Endl;} if (result! = NULL) mysql_free_result (result); // release the result resource mysql_close (& myCont); // disconnect return 0 ;}