Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6555502
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: Mysql/postgreSQL

2015-12-05 18:11:47

http://asyty.iteye.com/blog/1447092

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

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

 

连接数据库


点击(此处)折叠或打开

  1. #include <stdlib.h>
  2.     #include <stdio.h>
  3.     #include <mysql.h>
  4.     int main() {
  5.         MYSQL *conn_ptr;
  6.       
  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.         //root 为用户名 123456为密码 test为要连接的database
  14.       
  15.         if (conn_ptr) {
  16.             printf("Connection success\n");
  17.         } else {
  18.             printf("Connection failed\n");
  19.         }
  20.         mysql_close(conn_ptr);
  21.         return EXIT_SUCCESS;
  22.     }
通过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) 表示要连接的头文件目录,因为使用了 ,-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.       
  11.         conn_ptr = mysql_init(NULL);
  12.         if (!conn_ptr) {
  13.             return EXIT_FAILURE;
  14.         }
  15.         conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, NULL, 0);
  16.         if (conn_ptr) {
  17.             res = mysql_query(conn_ptr, "select name,age from user"); //查询语句
  18.             if (res) {
  19.                 printf("SELECT error:%s\n",mysql_error(conn_ptr));
  20.             } else {
  21.                 res_ptr = mysql_store_result(conn_ptr); //取出结果集
  22.                 if(res_ptr) {
  23.                     printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
  24.                     j = mysql_num_fields(res_ptr);
  25.                     while((sqlrow = mysql_fetch_row(res_ptr))) { //依次取出记录
  26.                         for(i = 0; i < j; i++)
  27.                             printf("%s\t", sqlrow[i]); //输出
  28.                         printf("\n");
  29.                     }
  30.                     if (mysql_errno(conn_ptr)) {
  31.                         fprintf(stderr,"Retrive error:s\n",mysql_error(conn_ptr));
  32.                     }
  33.                 }
  34.                 mysql_free_result(res_ptr);
  35.             }
  36.         } else {
  37.             printf("Connection failed\n");
  38.         }
  39.         mysql_close(conn_ptr);
  40.         return EXIT_SUCCESS;
  41.     }

插入更新及删除


点击(此处)折叠或打开

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








阅读(986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~