在 Linux 系统中,如果你想要使用 C 语言 与 MySQL 进行交互,通常需要使用 MySQL C API 或者 MySQL Connector/C,这是 MySQL 官方提供的 C 语言接口。
? 一、MySQL C API(原生接口)
MySQL C API 是 MySQL 官方提供的 C 语言接口,适用于 Linux、Windows、macOS 等系统。
1. 安装 MySQL C API
在 Linux 系统上安装 MySQL C API,通常需要安装 MySQL 的开发包:
sudo apt-get install libmysql-dev # Debian/Ubuntu
sudo yum install mysql-devel # CentOS/RHEL
2. 编写一个简单的 C 程序连接 MySQL
以下是一个基本的 C 程序示例,连接 MySQL 数据库并查询数据:
#include <stdio.h>
#include <mysql.h>int main() {MYSQL *conn;MYSQL_RES *res;MYSQL_ROW row;// 初始化连接conn = mysql_init(NULL);if (!mysql_real_connect(conn, + #引号 + localhost + #引号 + , + #引号 + username + #引号 + , + #引号 + password + #引号 + , + #引号 + database + #引号 + , NULL, NULL, 0)) {fprintf(stderr, + #引号 + Failed to connect to MySQL: %sn + #引号 + , mysql_error(conn));return 1;}// 查询数据if (mysql_query(conn, + #引号 + SELECT * FROM your_table LIMIT 10 + #引号 + )) {fprintf(stderr, + #引号 + Query failed: %sn + #引号 + , mysql_error(conn));return 1;}res = mysql_store_result(conn);if (res) {printf( + #引号 + Found %d rows:n + #引号 + , mysql_num_rows(res));for (int i = 0; i < mysql_num_rows(res); i++) {row = mysql_fetch_row(res);printf( + #引号 + Row %d: %sn + #引号 + , i, row[0]);}mysql_free_result(res);} else {fprintf(stderr, + #引号 + No rows found.n + #引号 + );}// 关闭连接mysql_close(conn);return 0;
}
3. 编译并运行
gcc -o mysql_test mysql_test.c -lmysqlclient
./mysql_test? 二、MySQL Connector/C(推荐)
MySQL Connector/C 是 MySQL 官方提供的 C 语言连接器,功能更强大、更稳定,推荐使用。
1. 安装 MySQL Connector/C
在 Linux 上安装:
sudo apt-get install libmysql-connector-c-dev # Debian/Ubuntu
sudo yum install mysql-connector-c-devel # CentOS/RHEL
2. 编写使用 MySQL Connector/C 的 C 程序
以下是一个简单的 C 程序示例,使用 MySQL Connector/C 连接数据库:
#include <mysql.h>
#include <stdio.h>int main() {MYSQL *conn;MYSQL_STMT *stmt;MYSQL_BIND bind[1];// 初始化连接conn = mysql_init(NULL);if (!mysql_real_connect(conn, + #引号 + localhost + #引号 + , + #引号 + username + #引号 + , + #引号 + password + #引号 + , + #引号 + database + #引号 + , NULL, NULL, 0)) {fprintf(stderr, + #引号 + Failed to connect to MySQL: %sn + #引号 + , mysql_error(conn));return 1;}// 创建 SQL 语句stmt = mysql_stmt_init(conn);if (!stmt) {fprintf(stderr, + #引号 + Failed to initialize statement: %sn + #引号 + , mysql_error(conn));return 1;}// 编译 SQL 语句if (mysql_stmt_prepare(stmt, NULL, strlen( + #引号 + SELECT * FROM your_table LIMIT 10 + #引号 + ), + #引号 + SELECT * FROM your_table LIMIT 10 + #引号 + , -1)) {fprintf(stderr, + #引号 + Failed to prepare statement: %sn + #引号 + , mysql_error(conn));return 1;}// 绑定参数bind[0].type = MYSQL_TYPE_NULL;bind[0].length = 0;bind[0].is_null = 0;bind[0].value = NULL;// 执行查询if (mysql_stmt_execute(stmt)) {fprintf(stderr, + #引号 + Failed to execute statement: %sn + #引号 + , mysql_error(conn));return 1;}// 获取结果if (mysql_stmt_store_result(stmt)) {fprintf(stderr, + #引号 + Failed to store result: %sn + #引号 + , mysql_error(conn));return 1;}// 获取行数int num_rows = mysql_stmt_num_rows(stmt);printf( + #引号 + Found %d rows:n + #引号 + , num_rows);// 遍历结果while (mysql_stmt_fetch(stmt)) {MYSQL_ROW row = mysql_fetch_row(stmt);printf( + #引号 + Row: %sn + #引号 + , row[0]);}// 释放资源mysql_stmt_close(stmt);mysql_close(conn);return 0;
}
3. 编译并运行
gcc -o mysql_connector mysql_connector.c -lmysqlclient
./mysql_connector? 三、其他注意事项
如果你使用的是 MySQL 8,建议使用 MySQL Connector/C 8。 如果你使用的是 MariaDB,也可以使用 MySQL Connector/C。 如果你使用的是 C++,推荐使用 MySQL Connector/C++,它更强大、更易用。? 四、总结
方法 推荐度 优点 缺点MySQL C API 一般 简单、轻量 功能较基础MySQL Connector/C 高 功能强大、稳定 需要额外安装MySQL Connector/C++ 高 更强大、支持更多功能 需要额外安装如果你有具体的问题(如连接数据库、执行 SQL、处理结果等),可以告诉我,我可以提供更详细的示例和说明。
文章转自:[《潜行者2:切尔诺贝利之心》海绵干怎么拿到 ] (https://www.52mwyd.com/news/14517.html)
作者:游戏攻略,转载请注明原文链接:https://www.52mwyd.com/