C 连接MySQL实例

1. 必须安装的软件

yum -y install gcc

yum -y install mysql-server

yun -y install mysql-devel

yum install -y zlib-devel

2. 连接数据库

#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>
int main() {
    MYSQL *conn_ptr;
 
    conn_ptr = mysql_init(NULL);
    if (!conn_ptr) {
        printf("mysql_init failed\n");
        return EXIT_FAILURE;
    }
    conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","123456","student",NULL,0);
    //root 为用户名 123456为密码 test为要连接的database
 
    if (conn_ptr) {
        printf("Connection success\n");
    } else {
        printf("Connection failed\n");
    }
    mysql_close(conn_ptr);
    return EXIT_SUCCESS;
}

3. 编译方法

gcc -g -o bbb -I/usr/include/mysql/ bbb.c -L/usr/lib64/mysql/ -lmysqlclient -lz

参数说明:-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文件

4. 查询代码  

#include <stdio.h>  
#include <stdlib.h>  
#include <mysql.h>

int main() {  
    MYSQL *conn_ptr;  
    MYSQL_RES *res_ptr;  
    MYSQL_ROW sqlrow;  
    MYSQL_FIELD *fd;  
    int res,i,j;  
  
    conn_ptr = mysql_init(NULL);  
    if (!conn_ptr) {  
        return EXIT_FAILURE;  
    }  
    conn_ptr = mysql_real_connect(conn_ptr,0);  
    if (conn_ptr) {  
        res = mysql_query(conn_ptr,"select id,name,serial,address from information"); //查询语句  
        if (res) {         
            printf("SELECT error:%s\n",mysql_error(conn_ptr));     
        } else {        
            res_ptr = mysql_store_result(conn_ptr);             //取出结果集  
            if(res_ptr) {               
                printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));   
                j = mysql_num_fields(res_ptr);          
                while((sqlrow = mysql_fetch_row(res_ptr)))  {   //依次取出记录  
                    for(i = 0; i < j; i++)         
                        printf("%s\t",sqlrow[i]);              //输出  
                    printf("\n");          
                }              
                if (mysql_errno(conn_ptr)) {                      
                    fprintf(stderr,"Retrive error:s\n",mysql_error(conn_ptr));               
                }        
            }        
            mysql_free_result(res_ptr);        
        }  
    } else {  
        printf("Connection failed\n");  
    }  
    mysql_close(conn_ptr);  
    return EXIT_SUCCESS;  
}  

5. 插入更新及删除

#include <stdlib.h>  
#include <stdio.h>  
#include <mysql.h>  
int main() {  
    MYSQL *conn_ptr;  
    int res;  
  
    conn_ptr = mysql_init(NULL);  
    if (!conn_ptr) {  
        printf("mysql_init failed\n");  
        return EXIT_FAILURE;  
    }  
    conn_ptr = mysql_real_connect(conn_ptr,0);  
    if (conn_ptr) {  
        //insert into information values(1,"wk","123","nmg");
        res = mysql_query(conn_ptr,"insert into information values(2,'Ann','555','sd')");   //可以把insert语句替换成delete或者update语句,都一样的  
//      res = mysql_query(conn_ptr,"delete from user where name = 'Ann' and age < 10");  
        if (!res) {     //输出受影响的行数  
            printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));   
        }  else {       //打印出错误代码及详细信息  
            fprintf(stderr,"Insert error %d: %sn",mysql_errno(conn_ptr),mysql_error(conn_ptr));  
        }  
    } else {  
        printf("Connection failed\n");  
    }  
    mysql_close(conn_ptr);  
    return EXIT_SUCCESS;  
}  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐