First of all, the most annoying problem is the various crashing environment configuration problems. After more than five hours of consultation, I finally solved the problem. Now I can't remember it, it makes no sense.
If the configuration is specific, record the process and write a blog post independently.
In short, I remember there are two key points to configure:
1) mysql. h path configuration or usage when using this header file
# Include "... mysql. H". This is an absolute path.
However, after my configuration is completed, I only need to write it as # include <MySQL/MySQL. h>.
2) When GCC is compiling, it needs to connect to the dynamic library configuration and forget how to configure it. In short, after writing the code for calling the database, the compiled command format is as follows:
Gcc a. C-o a-lmysqlclient (of course,-o a can not be understood) and then the executable file a. Out is successfully generated. There is no problem in running the file.
Here-The lmysqlclient seems to be connected to the dynamic library file, and I don't quite understand it. It will also be used just now.
The following problems may occur to Ubuntu users:
When installing MySQL, it may be installed in the Ubuntu Software Center. When you enter MySQL search in the software center, there will be many related things, such as MySQL server and MYSQL client.
Previously, I only installed MySQL server, but it was still difficult. Later I installed MYSQL client in various Debugging Processes, and then it went normal, I don't know the specific reason. I can only say it is possible. I have wasted a lot of time, but I hope that if someone is wasting time like me, it will save you some time.
========================================================== ========================
The following is a piece of simple code that I successfully tested. I have to paste it in a rush, and I can understand all the annotations.
It is to create a connection in the main function, then call a table creation function, then call a data insertion function, and finally call a display function (I am writing this function blindly, the output information is not displayed on the terminal, just for testing). The previous section is a simple implementation of the three simple functions, and the implementation method is not very good, for personal information, see.
//// // VI test. C, you know
#include<stdio.h>#include<mysql/mysql.h>void createTable(MYSQL* my){int ret;ret=mysql_query(my,"create table mytable(name varchar(20) ,gender varchar(1) not null)");if(ret){printf("create table failed!\n");return ;}printf("create table success!\n");}void insert(MYSQL* my){int ret;ret=mysql_query(my,"insert into mytable values('CSDN','m')");if(ret){printf("insert failed!\n");return ;}printf("insert success!\n");}void output(MYSQL* my){int ret;ret=mysql_query(my,"select * from mytable ");if(ret){printf("select failed!\n");return ;}printf("select success!\n");}int main(){MYSQL* mysql;mysql=mysql_init(NULL);if(!mysql){fprintf(stderr,"mysql_init error!\n");return 0;}printf("mysql_init success!\n");mysql=mysql_real_connect(mysql,"localhost","root","123","mydatabase",0,NULL,0);if(!mysql){fprintf(stderr,"mysql_connect error!\n");return 0;}printf("mysql connect success!\n");createTable(mysql);insert(mysql);output(mysql);mysql_close(mysql);return 0;}
Then exit the editor and compile it. The command line is as follows:
GCC test. C-o Test-lmysqlclient is compiled successfully. The executable file test is generated successfully, and the executable file execution is correct.
Open a terminal, log on to the database, view the database, tables, and so on, and see that the table is created successfully, and the data is inserted successfully, as expected.
Since then, the hateful environment configuration has come to an end...