
迷途小书童
读完需要
10
分钟速读仅需 4 分钟
1
简介
MySQL 是一种流行的关系型数据库管理系统,而 C 语言是一种强大的编程语言,可以与 MySQL 进行交互。本篇博文将介绍如何使用 C 语言来连接、查询和操作 MySQL 数据库。我们将涵盖原理实现、安装步骤、使用示例和代码解析,帮助您快速上手使用 C 语言操作 MySQL 数据库。
2
原理实现
C 语言操作 MySQL 数据库的原理是通过 MySQL 提供的 C API(Application Programming Interface)来实现。C API 是一组函数和数据结构,允许开发人员使用 C 语言与 MySQL 进行通信。通过这些函数,我们可以连接到 MySQL 服务器,执行 SQL 操作语句,并处理返回的结果。
3
安装步骤
在开始之前,您需要确保已经安装了 MySQL 数据库和相应的 C 语言开发环境。以下是安装步骤的概述
安装 MySQL 数据库:根据您的操作系统,下载并安装适用于您的 MySQL 数据库版本
安装 MySQL Connector/C:MySQL Connector/C 是 MySQL 提供的用于 C 语言的官方驱动程序
配置编译器:确保您的 C 语言编译器已正确配置,并能够链接 MySQL Connector/C 库
对应到 ubuntu 系统中,命令是
sudo apt install mysql-server mysql-workbench libmysqlclient-dev build-essential
4
增删改查操作
基本的注释都写在了代码里了,应该都很容易看懂,主要是几个重要的 API 的调用。
首先创建数据库 db 并建一张表, 表结构如下

看看代码
- #include
h> - #include
h> - #include
h> - #include
h> -
-
- #define MYSQL_HOST "localhost"
- #define MYSQL_USER "root"
- #define MYSQL_PASSWD "toor"
- #define DB_NAME "db"
- #define TABLE_NAME "students"
-
-
- /*
- * 测试的数据表结构很简单, number, grade, sex, score 四个字段, 代表学号、年级、性别、分数
- */
-
-
- /* 连接mysql */
- void mysqldb_connect(MYSQL *mysql)
- {
- if(!mysql_real_connect(mysql, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, DB_NAME, 0, NULL, 0)) {
- printf("\nFailed to connect:%s\n", mysql_error(mysql));
- } else {
- printf("\nConnect sucessfully!\n");
- }
- }
-
-
- /* 插入数据 */
- void mysqldb_insert(MYSQL *mysql, int number, int grade, char *sex, int score)
- {
- int t;
- char *head = "INSERT INTO ";
- char query[120];
- char field[48] = "number, grade, sex, score";
- char *left = "(";
- char *right = ") ";
- char *values = "VALUES";
- char message[100] = {0};
-
-
- sprintf(message, "%d, %d, \"%s\", %d", number, grade, sex, score);
-
-
- /* 拼接sql命令 */
- sprintf(query, "%s%s%s%s%s%s%s%s%s", head, TABLE_NAME, left, field, right, values, left, message, right);
- printf("%s\n", query);
-
-
- t = mysql_real_query(mysql, query, strlen(query));
- if (t) {
- printf("Failed to query: %s\n", mysql_error(mysql));
- }
- else {
- printf("\nInsert sucessfully!\n");
- }
-
- }
-
-
- /* 删除数据 */
- void mysqldb_delete(MYSQL *mysql, char *field_name, int number)
- {
- int t;
- char *head = "DELETE FROM ";
- char query[120];
-
-
- sprintf(query, "%s%s where %s =%d", head, TABLE_NAME, field_name, number);
- printf("%s\n", query);
-
-
- t = mysql_real_query(mysql, query, strlen(query));
- if (t) {
- printf("\nFailed to query: %s\n", mysql_error(mysql));
- } else {
- printf("\nDelete data sucessfully!\n");
- }
-
- }
-
-
- /* 更新数据 */
- void mysqldb_update(MYSQL *mysql, char *field_name, int score)
- {
- int t;
- char *head = "UPDATE ";
- char query[100];
-
-
- sprintf(query, "%s%s SET %s=%d", head, TABLE_NAME, field_name, score);
- printf("%s\n", query);
-
-
- t = mysql_real_query(mysql, query, strlen(query));
- if (t) {
- printf("Failed to update: %s\n", mysql_error(mysql));
- return;
- }
- printf("\nUpdate data sucessfully!\n");
- }
-
-
- /* 查询数据 */
- void mysqldb_query(MYSQL *mysql)
- {
- int t;
- char *head = "SELECT * FROM ";
- char query[50] = {0};
- MYSQL_RES *res;
- MYSQL_ROW row;
-
-
- sprintf(query, "%s%s", head, TABLE_NAME);
-
-
- t = mysql_real_query(mysql, query, strlen(query));
-
-
- if (t) {
- printf("Failed to query: %s\n", mysql_error(mysql));
- return;
- } else {
- printf("\nQuery successfully!\n");
- }
-
-
- res = mysql_store_result(mysql);
- while (row = mysql_fetch_row(res)) {
- for(t = 0; t < mysql_num_fields(res); t++) {
- printf("%s\t", row[t]);
- }
- printf("\n");
- }
- mysql_free_result(res);
- }
-
-
- /* 断开mysql连接 */
- void close_connection(MYSQL *mysql)
- {
- mysql_close(mysql);
- }
-
-
- int main(int argc, char *argv[])
- {
- // 准备一组数据
- int number = 1;
- int grade = 1;
- char sex[] = "male";
- int score = 100;
-
-
- // 初始化mysql
- MYSQL *mysql = mysql_init(NULL);
- if (!mysql) {
- printf("\nMysql init failed.\n");
- }
-
-
- // 连接MYSQL
- mysqldb_connect(mysql);
-
-
- // 插入数据
- mysqldb_insert(mysql, number, grade, sex, score);
-
-
- // 更新数据
- mysqldb_update(mysql, "score", 99);
-
-
- // 查询数据
- mysqldb_query(mysql);
-
-
- // 删除数据
- mysqldb_delete(mysql, "number", number);
-
-
- // 断开连接
- close_connection(mysql);
-
-
- return 0;
- }
四个操作,在执行前面的操作时,后面暂未执行的可以先注释掉,然后一步步查看结果,看是否和预期的一致。
5
编译
使用 gcc 进行编译,命令如下
gcc -o mysql_in_c mysql_in_c.c -lmysqlclient
生成可执行文件 mysql_in_c
6
执行结果
增加一条记录

更改一条记录

查询记录

删除记录

7
参考资料
https://github.com/mysql
8
免费社群

