• Linux C语言连接MySQL 增删改查操作


    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 ");
    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 ");
    17.         } else {
    18.             printf("Connection failed ");
    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) 表示要连接的头文件目录,因为使用了<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.       
    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 ",mysql_error(conn_ptr));
    20.             } else {
    21.                 res_ptr = mysql_store_result(conn_ptr); //取出结果集
    22.                 if(res_ptr) {
    23.                     printf("%lu Rows ",(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 ", sqlrow[i]); //输出
    28.                         printf(" ");
    29.                     }
    30.                     if (mysql_errno(conn_ptr)) {
    31.                         fprintf(stderr,"Retrive error:s ",mysql_error(conn_ptr));
    32.                     }
    33.                 }
    34.                 mysql_free_result(res_ptr);
    35.             }
    36.         } else {
    37.             printf("Connection failed ");
    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 ");
    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 ",(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 ");
    24.         }
    25.         mysql_close(conn_ptr);
    26.         return EXIT_SUCCESS;
    27.     }








    <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>
    阅读(36) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    oracle的根容器下新建pdb容器及本地用户
    oracle监听配置与防火墙问题
    oracle问题:ORA-09817及解决办法
    Oracle:Ora-01652无法通过128(在temp表空间中)扩展temp段的过程-解决步骤
    oracle:ORA-14765建索引阻塞创建分区及处理步骤
    oracle-组合索引字段位置与查询效率之间的关系
    hbase的split策略和预分区
    启动hbase后hmaster自动关闭
    hive一级分区、二级分区、动态分区
    hive beeline连接和交互shell连接
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649351.html
Copyright © 2020-2023  润新知