C语言怎么链接mysql数据库

在C语言中连接数据库通常通过调用数据库提供的API来实现,比如MySQL的C API。以下是一个使用MySQL C API连接数据库并获取数据的示例。

前提条件

  1. 安装MySQL服务器和客户端。
  2. 安装MySQL开发库,例如libmysqlclient-dev。
  3. 确保您的开发环境中已经安装了MySQL的头文件和库文件。

示例代码

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

void finish_with_error(MYSQL *con) {
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
}

int main() {
    MYSQL *con = mysql_init(NULL);

    if (con == NULL) {
        fprintf(stderr, "mysql_init() failed\n");
        exit(1);
    }

    if (mysql_real_connect(con, "localhost", "your_user", "your_password", "your_database", 0, NULL, 0) == NULL) {
        finish_with_error(con);
    }

    if (mysql_query(con, "SELECT * FROM your_table")) {
        finish_with_error(con);
    }

    MYSQL_RES *result = mysql_store_result(con);

    if (result == NULL) {
        finish_with_error(con);
    }

    int num_fields = mysql_num_fields(result);

    MYSQL_ROW row;
    MYSQL_FIELD *field;

    while ((row = mysql_fetch_row(result))) {
        for(int i = 0; i < num_fields; i++) {
            if (i == 0) {
               while(field = mysql_fetch_field(result)) {
                   printf("%s ", field->name);
               }
               printf("\n");
            }
            printf("%s  ", row[i] ? row[i] : "NULL");
        }
        printf("\n");
    }

    mysql_free_result(result);
    mysql_close(con);

    exit(0);
}

编译和运行

在Linux系统中,可以使用以下命令编译和运行这个C程序。假设保存的文件名为mysql_example.c

gcc -o mysql_example mysql_example.c `mysql_config --cflags --libs`
./mysql_example

代码解释

  1. 包含头文件

    #include <mysql/mysql.h>
    

    这个头文件包含了MySQL C API的所有函数和数据结构。

  2. 初始化MySQL连接

    MYSQL *con = mysql_init(NULL);
    

    初始化一个MYSQL对象。

  3. 连接到数据库

    if (mysql_real_connect(con, "localhost", "your_user", "your_password", "your_database", 0, NULL, 0) == NULL) {
        finish_with_error(con);
    }
    

    使用mysql_real_connect函数连接到数据库。参数依次为:主机名、用户名、密码、数据库名、端口号、套接字和标志。

  4. 执行查询

    if (mysql_query(con, "SELECT * FROM your_table")) {
        finish_with_error(con);
    }
    

    使用mysql_query函数执行SQL查询。

  5. 存储查询结果

    MYSQL_RES *result = mysql_store_result(con);
    

    使用mysql_store_result函数将查询结果存储在MYSQL_RES结构中。

  6. 获取并打印结果

    int num_fields = mysql_num_fields(result);
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))) {
        for(int i = 0; i < num_fields; i++) {
            printf("%s  ", row[i] ? row[i] : "NULL");
        }
        printf("\n");
    }
    

    使用mysql_fetch_row函数逐行获取查询结果并打印。

  7. 释放结果和关闭连接

    mysql_free_result(result);
    mysql_close(con);
    

    使用mysql_free_result函数释放结果集,并使用mysql_close函数关闭数据库连接。

处理错误

使用finish_with_error函数处理错误。如果发生错误,打印错误信息并关闭连接:

void finish_with_error(MYSQL *con) {
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
}

这样就可以通过C语言连接MySQL数据库并获取数据。确保替换示例中的your_useryour_passwordyour_databaseyour_table为实际的数据库用户名、密码、数据库名和表名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好玩的Matlab(NCEPU)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值