preface:
A few days ago, I received a list of comprehensive homework of C language data structure and algorithm at the end of the term. The customer asked to do a small game of watching continuously, and asked to use at least four algorithms to realize the functions. After completing the game, the player recorded the scores in the MySQL database. Before that, I had never used C language to connect to the database. I could only connect to the database in Java and Python. At the beginning, I went to ask the embedded senior how to connect to the database in C language. As a result, he couldn't
Then I can only Baidu, but what Baidu others wrote is not detailed enough. I introduced MySQL as required h, libmysql. Lib still reports various errors, which makes my mind a little broken. I hereby write down the detailed implementation of C language connection to MySQL database under different operating systems.
Windows Environment:
1, Prerequisite preparation:
MySQL 5.7 (64 bit)
Visual Studio 2019 (other compilers can also be used. It can be compiled with 64 bits, which corresponds to the number of bits of MySQL)
2, Import file
C language is cumbersome to connect to the database. It is necessary to introduce header files and dependencies about connecting to the database.
Here comes the question: where did all these documents come from? Where can I find these header files, dependencies. Please continue to read:
Find the location where MySQL was installed at that time: (my MySQL installation path: C:\Program Files\MySQL\MySQL Server 5.7), and you will see the following directory:
You can copy the files in the same directory as the lib and include files.
All items in the include folder should be copied, not just mysql h. Because mysql H needs to rely on other header files. Copy libmysql.exe in the lib folder DLL and libmysql lib.
3, Test code writing:
#include <stdio.h> #include <string.h> #include <stdlib.h> /*The header file and lib package for connecting Mysql are introduced*/ #include "mysql/mysql.h" #pragma comment(lib,"libmysql") /*Define some macros required for database connection*/ #define HOST "localhost" /*MySql server address*/ #define USERNAME "root" / * username*/ #define PASSWORD "123456" / * database connection password*/ #define DATABASE "rank" / * database to be connected*/ // Functions that execute sql statements void exeSql(char* sql) { MYSQL my_connection; /*Database connection*/ int res; /*Return flag after executing sql statement*/ MYSQL_RES* res_ptr; /*results of enforcement*/ MYSQL_ROW result_row; /*Return query information by row*/ int row, column; /* Define the number of rows and columns*/ mysql_init(&my_connection); if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) { printf("Database connection succeeded!"); /*Set the query code to utf8 and support Chinese*/ mysql_query(&my_connection, "set names utf8"); res = mysql_query(&my_connection, sql); if (res) { /*Now it means the execution failed*/ printf("Error: mysql_query !\n"); /*Don't forget to close the connection*/ mysql_close(&my_connection); } else { /*Now it means successful implementation*/ /*mysql_affected_rows The number of rows affected after sql execution will be returned*/ printf("%d Line affected!\n", mysql_affected_rows(&my_connection)); // Load the query results into res_ptr res_ptr = mysql_store_result(&my_connection); // Output if present if (res_ptr) { // Get the number of rows and columns row = mysql_num_rows(res_ptr); column = mysql_num_fields(res_ptr); // Execute the output result and cycle from the second line (the first line is the field name) for (int i = 1; i < row + 1; i++) { // One line of data result_row = mysql_fetch_row(res_ptr); for (int j = 0; j < column; j++) { printf("%s", result_row[j]); } } } /*Don't forget to close the connection*/ mysql_close(&my_connection); } } else { printf("Database connection failed!"); } }
Linux environment:
1, Prerequisite preparation:
MySql 5.7 (64 bit)
c/c + + environment (gcc) compilation environment
Start Mysql service: sudo service start mysql
2, Test code writing:
be careful:
- If MySQL cannot find mysql h. You can install apt get install libmysqlclient dev. after the installation, re-enter the MySQL directory and there will be the corresponding header file.
- Mysql.com is introduced into Linux H should not be placed directly in the same directory as the main file as in windows environment, but find mysql H location: / usr/include/mysql/
#include <stdio.h> #include <string.h> #include <stdlib.h> /*Import header file for connecting to Mysql*/ #include "/usr/include/mysql/mysql.h" /*Define some macros required for database connection*/ #define HOST "localhost" /*MySql server address*/ #define USERNAME "root" / * username*/ #define PASSWORD "123456" / * database connection password*/ #define DATABASE "rank" / * database to be connected*/ // Functions that execute sql statements void exeSql(char* sql) { MYSQL my_connection; /*Database connection*/ int res; /*Return flag after executing sql statement*/ MYSQL_RES* res_ptr; /*results of enforcement*/ MYSQL_ROW result_row; /*Return query information by row*/ int row, column; /* Define the number of rows and columns*/ mysql_init(&my_connection); if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) { printf("Database connection succeeded!"); /*Set the query code to utf8 and support Chinese*/ mysql_query(&my_connection, "set names utf8"); res = mysql_query(&my_connection, sql); if (res) { /*Now it means the execution failed*/ printf("Error: mysql_query !\n"); /*Don't forget to close the connection*/ mysql_close(&my_connection); } else { /*Now it means successful implementation*/ /*mysql_affected_rows The number of rows affected after sql execution will be returned*/ printf("%d Line affected!\n", mysql_affected_rows(&my_connection)); // Load the query results into res_ptr res_ptr = mysql_store_result(&my_connection); // Output if present if (res_ptr) { // Get the number of rows and columns row = mysql_num_rows(res_ptr); column = mysql_num_fields(res_ptr); // Execute the output result and cycle from the second line (the first line is the field name) for (int i = 1; i < row + 1; i++) { // One line of data result_row = mysql_fetch_row(res_ptr); for (int j = 0; j < column; j++) { printf("%s", result_row[j]); } } } /*Don't forget to close the connection*/ mysql_close(&my_connection); } } else { printf("Database connection failed!"); } }
Compiler: GCC - G mysqlconnectdemo C - lmysqlclient (must bring - lmysqlclient)