C 连接mysql

      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 //#include <mysql/mysql.h>
      4 #include</usr/local/mysql/include/mysql.h>
      5 int main(int argc, char *argv[]) {
      6     MYSQL *conn_ptr;  
      7     conn_ptr = mysql_init(NULL);
      8     if (!conn_ptr) {   
      9         fprintf(stderr, "mysql_init failed/n");
     10         return EXIT_FAILURE;
     11     }  
     12     conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","",
     13             "DB_Test",0,NULL,0);
     14     if (conn_ptr) {  
     15         printf("Connection success/n");
     16     } else {  
     17         printf("Connection failed/n");
     18     }  
     19     mysql_close(conn_ptr);
     20     return EXIT_SUCCESS;
     21 }

 

编译链接

gcc -o connection -g connection.c  -lz /usr/local/mysql/lib/libmysqlclient.so.15.0.0

注意事项4 #include</usr/local/mysql/include/mysql.h> 找到mysql.h文件

gcc 找到 libmysqlclient.so.15.0.0库

 

 

你可能感兴趣的:(c,mysql,gcc,null)