1. C onnect MySQL database with C + +
First, create a new C + + project in VS, right-click the project name, and select properties.
Select platform selection
Select configuration manager
Choose new
Select X64 from the drop-down menu. OK
Select C / C + + > General - > attach the include directory, and add C:\Program Files\MySQL\MySQL Server 5.5\include (select according to your own installation directory)
Select connector - > General - > additional library directory. Add C:\Program Files\MySQL\MySQL Server 5.5\lib; (select according to your own installation directory)
Select connector - > Input - > attach dependency. Add C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib; (select according to your own installation directory)
Finally, the dynamic link library libmysql.dll is copied to the X64 generation directory of the project. The dynamic connection library file is in the directory of C:\Program Files\MySQL\MySQL Server 5.5\lib \
2. C + + uses MySQL database
Sample program, digested by itself, contains API s that Google doesn't understand
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<Windows.h> #include<WinSock.h> #include<mysql.h> using namespace std; #pragma comment(lib,"libmysql.lib") #pragma comment(lib,"wsock32.lib") MYSQL *mysql = new MYSQL; //mysql connection MYSQL_FIELD *fd; //Field column array char field[32][32]; //Save field name 2D array MYSQL_RES *res; //This structure represents a query result set of returned rows MYSQL_ROW column; //A type safe representation of row data, representing the columns of the row char query[150]; //Query statement bool ConnectDatabase(); bool QueryDatabase1(); //bool QueryDatabase2(); int main() { ConnectDatabase(); QueryDatabase1(); //QueryDatabase2(); system("pause"); return 0; } bool ConnectDatabase() { //Initialize mysql mysql_init(mysql); //If false is returned, the connection fails. If true is returned, the connection succeeds if (!(mysql_real_connect(mysql, "localhost", "root", "123456", "company", 0, NULL, 0))) //In the middle are the host, user name, password, database name, port number (you can write the default 0 or 3306, etc.), which can be written as parameters first and then passed in { printf("Error connecting to database:%s\n", mysql_error(mysql)); return false; } else { printf("Connected...\n"); return true; } return true; } bool QueryDatabase1() { sprintf_s(query, "select * from t_dept"); //Execute the query statement. Here is query all. user is the table name, without quotation marks. You can also use strcpy mysql_query(mysql, "set names gbk"); //Set the encoding format (SET NAMES GBK also works), otherwise, the Chinese code will be garbled under cmd //Return 0 query success, return 1 query failure if (mysql_query(mysql, query)) //Execute SQL statement { printf("Query failed (%s)\n", mysql_error(mysql)); return false; } else { printf("query success\n"); } //Get result set if (!(res = mysql_store_result(mysql))) //Get the result set returned after the end of sql statement { printf("Couldn't get result from %s\n", mysql_error(mysql)); return false; } //Print data lines printf("number of dataline returned: %d\n", mysql_affected_rows(mysql)); //Get field information char *str_field[32]; //Define a string array to store field information for (int i = 0; i<4; i++) //Get field name with known number of fields { str_field[i] = mysql_fetch_field(res)->name; } for (int i = 0; i<4; i++) //Print field printf("%10s\t", str_field[i]); printf("\n"); //Print acquired data while (column = mysql_fetch_row(res)) //Get and print the next line given the number of fields { printf("%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3]); //column is an array of columns } return true; } //bool QueryDatabase2() //{ // mysql_query(mysql, "set names gbk"); // //Return 0 query success, return 1 query failure // If (MySQL [query (mysql, "select * from user")) / / execute the SQL statement // { // printf("Query failed (%s)\n", mysql_error(mysql)); // return false; // } // else // { // printf("query success\n"); // } // res = mysql_store_result(mysql); // //Print data lines // printf("number of dataline returned: %d\n", mysql_affected_rows(mysql)); // for (int i = 0; fd = mysql_fetch_field(res); i + +) / / get the field name // strcpy(field[i], fd->name); // Int j = MySQL num fields (RES); / / get the number of columns // For (int i = 0; I < J; I + +) / / print fields // printf("%10s\t", field[i]); // printf("\n"); // while (column = mysql_fetch_row(res)) // { // for (int i = 0; i<j; i++) // printf("%10s\t", column[i]); // printf("\n"); // } // return true; // //}