​​


Linux下想要测试mysql和memcached的性能,因为是服务器只能通过终端连接,所以考虑用C语言写测试代码。于是研究了把C怎么连接 MySQL以及增删改查的代码。安装mysql-client或者编译源码安装mysql后,会有支持C语言写客户端的头文件和库文件,但是目录可能不一 样,mysql源码安装见 ​​​​​

从网上找了类似的代码改改后如下


连接数据库




  1. #include <stdlib.h>
  2.     #include <stdio.h>
  3.     #include <mysql.h>
  4.     int main() {
  5.         MYSQL *conn_ptr;

  6.         conn_ptr = mysql_init(NULL);
  7.         if (!conn_ptr) {
  8.             printf("mysql_init failed\n");
  9.             return EXIT_FAILURE;
  10.         }
  11.         conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, NULL, 0);
  12.         //root 为用户名 123456为密码 test为要连接的database

  13.         if (conn_ptr) {
  14.             printf("Connection success\n");
  15.         } else {
  16.             printf("Connection failed\n");
  17.         }
  18.         mysql_close(conn_ptr);
  19.         return EXIT_SUCCESS;
  20.     }

通过gcc编译(我的mysql源码安装目录为 /usr/local/mysql)


gcc -o connect -g connect.c  -I /usr/local/mysql/include/ -L /usr/local/mysql/lib/ -l mysqlclient


参数说明:-I(大写的i) 表示要连接的头文件目录,因为使用了<mysql.h> ,-L表示要连接的库文件目录 -l(小写的L) 表示要连接的具体的库名称,在/usr/local/mysql/lib/ 下,有叫做libmysqlclient.so的库,指定具体的库的名字时,默认去掉头尾的lib和.so直接使用中间的mysqlclient

如果不用-I -L ,可能导致找不到头文件 或者 函数未定义的错误


如果出现错误error while loading shared libraries,那是因为.so没有被系统载入,解决办法如下

vi /etc/ld.so.conf // 打开ld.so.conf文件,增加.so文件的路径,比如/usr/local/mysql/lib/

ldconfig //重新加载.so文件


查询代码




  1. #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <mysql.h>
  4.     int main() {
  5.         MYSQL *conn_ptr;
  6.         MYSQL_RES *res_ptr;
  7.         MYSQL_ROW sqlrow;
  8.         MYSQL_FIELD *fd;
  9.         int res, i, j;

  10.         conn_ptr = mysql_init(NULL);
  11.         if (!conn_ptr) {
  12.             return EXIT_FAILURE;
  13.         }
  14.         conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, NULL, 0);
  15.         if (conn_ptr) {
  16.             res = mysql_query(conn_ptr, "select name,age from user"); //查询语句
  17.             if (res) {
  18.                 printf("SELECT error:%s\n",mysql_error(conn_ptr));
  19.             } else {
  20.                 res_ptr = mysql_store_result(conn_ptr); //取出结果集
  21.                 if(res_ptr) {
  22.                     printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
  23.                     j = mysql_num_fields(res_ptr);
  24.                     while((sqlrow = mysql_fetch_row(res_ptr))) { //依次取出记录
  25.                         for(i = 0; i < j; i++)
  26.                             printf("%s\t", sqlrow[i]); //输出
  27.                         printf("\n");
  28.                     }
  29.                     if (mysql_errno(conn_ptr)) {
  30.                         fprintf(stderr,"Retrive error:s\n",mysql_error(conn_ptr));
  31.                     }
  32.                 }
  33.                 mysql_free_result(res_ptr);
  34.             }
  35.         } else {
  36.             printf("Connection failed\n");
  37.         }
  38.         mysql_close(conn_ptr);
  39.         return EXIT_SUCCESS;
  40.     }


插入更新及删除




  1. #include <stdlib.h>
  2.     #include <stdio.h>
  3.     #include <mysql.h>
  4.     int main() {
  5.         MYSQL *conn_ptr;
  6.         int res;

  7.         conn_ptr = mysql_init(NULL);
  8.         if (!conn_ptr) {
  9.             printf("mysql_init failed\n");
  10.             return EXIT_FAILURE;
  11.         }
  12.         conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, NULL, 0);
  13.         if (conn_ptr) {
  14.             res = mysql_query(conn_ptr, "insert into user values(null,'Ann',5)"); //可以把insert语句替换成delete或者update语句,都一样的
  15.     // res = mysql_query(conn_ptr, "delete from user where name = 'Ann' and age < 10");
  16.             if (!res) { //输出受影响的行数
  17.                 printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
  18.             } else { //打印出错误代码及详细信息
  19.                 fprintf(stderr, "Insert error %d: %sn",mysql_errno(conn_ptr),mysql_error(conn_ptr));
  20.             }
  21.         } else {
  22.             printf("Connection failed\n");
  23.         }
  24.         mysql_close(conn_ptr);
  25.         return EXIT_SUCCESS;
  26.     }





<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

上一篇:​​MySQL C API by Example​

下一篇:​​mysql datetime 时间比较​

相关热门文章

给主人留下些什么吧!~~ 评论热议