MySQL 提供的C API ,应用的例子

本文介绍如何使用MySQL提供的C语言API进行数据库操作,包括连接数据库、执行查询并展示查询结果等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我又研究了一下 MySQL 提供的 C 语言的 API 因为上次我提供的页面有创建远程用户    以及 修改 mysql 用户名和密码的 所以 CGI 里面必须调用 mysql C API 才能完成。 经过确认, 这些都可以被 MySQL C API 所支持。


比如 create database create table modify table index 等等。

我们 CGI 里面所要做的就是 调用 MySQL C API 来修改 MySQL 数据库里面的 mysql 数据库里面的 user 表即可。

下面是个例子

1>
连接 MySQL 数据库,并 use mysql;
2> select * from user;   //
查询表里面的数据
3>
打印即可

编译:

gcc -o mysql_example mysql_example.c -I/usr/local/include/mysql -L /usr/local/lib/mysql/  -lmysqlclient  

执行结果:
#shell> ./mysql_example
CODE:
localhost        root        *E6CC90B878B948C35E92B003C792C46C58C4AF40        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       
boblinux        root                Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       
boblinux                        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N                                        0        0        0        0       
localhost                        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N                                        0        0        0        0       
%        admin        *4ACFE3202A5FF5CF467898FC58AAB1D615029441        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       

代码:
CODE:
/* 载入相关头文件 */
//#include <winsock2.h>
//
windows 平台,假设链接时发现很多链接错误的时候,需要加上对 winsock2.h 的引用。
#include <stdio.h>
#include <mysql/mysql.h>

main() {
       
        MYSQL mysql;
        MYSQL_RES *result;
       
        MYSQL_ROW row;
        int numrows, numcols, c;
        char query[] = "SELECT *  FROM user";
       
        mysql_init(&mysql);

        /*
连接到数据库 */

        if (!mysql_real_connect(&mysql, "172.21.5.179",        "admin", "admin",        "mysql", 0, NULL, 0))
        {
                //
访问失败输出错误信息
                fprintf(stderr,        "Failed to connect to database: Error %d:%s/n", mysql_errno(&mysql),mysql_error(&mysql));
                return -1;
        }

        /*
执行一个查询 */

        if (mysql_query(&mysql, query))
        {
                //
查询失败输出错误信息
                fprintf(stderr,        "Error executing query: Error %d: %s/n",                mysql_errno(&mysql), mysql_error(&mysql));
        }

        /*
处理查询结果 */
       
       
//        25.2.3.47. mysql_num_rows()
//        my_ulonglong mysql_num_rows(MYSQL_RES *result)
//       
//       
描述
//       
//       
返回结果集中的行数。
//       
//        mysql_num_rows()
的使用取决于是否采用了 mysql_store_result() mysql_use_result() 来返回结果集。如果使用了 mysql_store_result() ,可以立刻调用 mysql_num_rows() 。如果使用了 mysql_use_result() mysql_num_rows() 不返回正确的值,直至检索了结果集中的所有行为止。
//       
//       
返回值
//       
//       
结果集中的行数。
        result = mysql_store_result(&mysql);   //mysql_use_result()
mysql_store_result()

        if (!result)
        {
                //
查询结果出错
                fprintf(stderr,        "Error executing query: Error %d: %s/n", mysql_errno(&mysql), mysql_error(&mysql));
        }

        /*
查找查询结果的列数 */

        numcols = mysql_num_fields(result);
        numrows = mysql_num_rows(result);
       
        printf("filds = %d/n",numcols);
        printf("rows = %d/n",numrows);
        printf("/n/n");

        /*
循环显示查询结果 */

        while (row = mysql_fetch_row(result)) {
                for(c=0; c<numcols; c++) {
                        printf("%s/t", row[c]);
                }
                printf("/n");
        }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值