【Linux环境】【C语言开发】【mysql】Linux环境下C语言操作mysql

简介: 【Linux环境】【C语言开发】【mysql】Linux环境下C语言操作mysql

准备工作


  • 参考资料中《安装基础MySQL环境》;


两个要点


  • 头文件包含(解决编译问题)


#include <mysql/mysql.h>


包含该文件,需要提前安装mysql-devel包;


  • 动态库链接(解决链接问题)


/usr/lib64/mysql/libmysqlclient.so


需要在gcc或makefile中指定该so的路径;


主要api接口


- MYSQL *mysql_init(MYSQL *mysql);
- MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);
- int mysql_real_query(MYSQL *mysql, const char *query, unsigned long length);
- MYSQL_RES *mysql_store_result(MYSQL *mysql)
- MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
- unsigned int mysql_num_fields(MYSQL_RES *result);
- void mysql_close(MYSQL *mysql);
- void mysql_free_result(MYSQL_RES *result);


demo测试代码及编译命令


  • demo版测试代码如下:


 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <mysql/mysql.h>
 #define MAX_BUF_SIZE 2018 
 MYSQL *g_conn; 
 MYSQL_RES *g_res; 
 MYSQL_ROW g_row;
 const char *g_host_name = "localhost";
 const char *g_user_name = "root";
 const char *g_password = "Cyg26032583.";
 const char *g_db_name = "qxhgdfirstdb";
 const unsigned int g_db_port = 3306;
 void print_mysql_error(const char *msg) {
     if (msg)
         printf("%s: %s\n", msg, mysql_error(g_conn));
     else
         puts(mysql_error(g_conn));
 }
 int execute_mysql(const char * sql) {
     if (mysql_real_query(g_conn, sql, strlen(sql))) 
         return -1; 
     return 0; 
 }
 int init_mysql() { 
     g_conn = mysql_init(NULL);
     if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) 
         return -1;
     if (execute_mysql("set names utf8")) 
         return -1;
     return 0; 
 }
 int main(void) {
     int iNum_rows;
     int iNum_fields;
     if (init_mysql());
         print_mysql_error(NULL);
     if (execute_mysql("insert into employeelst values(2,'chunmei'); "))
         print_mysql_error(NULL);
     if (execute_mysql("SELECT * FROM employeelst;")) 
         print_mysql_error(NULL);
     g_res = mysql_store_result(g_conn); 
     iNum_rows   = mysql_num_rows(g_res); 
     iNum_fields = mysql_num_fields(g_res); 
     printf("total %d records,each record %d datafields as follows:\n", iNum_rows, iNum_fields);
     printf("id\tname\n");
     while ((g_row = mysql_fetch_row(g_res))) 
         printf("%s\t%s\n", g_row[0], g_row[1]); 
     mysql_free_result(g_res); 
     mysql_close(g_conn); 
     return 0;
 }


  • 编译命令如下所示:


gcc mysql_test.c -L /usr/lib64/mysql -lmysqlclient


  • 测试输出如下:


[qxhgd@localhost mysql]$ gcc mysql_test.c -L /usr/lib64/mysql -lmysqlclient;./a.out
total 15 records,each record 2 datafields as follows:
id      name
1       秋香
9527    华安
2       chunmei


遇到问题及解决方式


  • 问题1、fatal error: mysql/mysql.h: No such file or directory


## 编译报错打印
mysql_test.c:15:26: fatal error: mysql/mysql.h: No such file or directory
  #include <mysql/mysql.h>
                          ^
compilation terminated.
## 解决方案
yum install mysql-devel 


  • 问题2、cannot find -lmysqlclient


## 编译报错打印
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
## 解决方案
gcc mysql_test.c -L /usr/lib64/mysql -lmysqlclient 


参考资料



相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
10月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
1016 11
|
12月前
|
存储 监控 Linux
Linux环境锁定关键文件防止误删操作流程。
总结以上内容,在Linux环境下锁定重要文档避免误删涉及到多种技术手段与策略组合运作, 包括但不限于利用chatter指挥官固化文档状态至只读模式、运作ACL精准调整访问权利列表、编排自动化流程简
551 20
|
12月前
|
Linux
Linux环境下的UDEV机制及其与守护进程的关联
实际使用时管理员需要熟悉编写合适udev rules去满足特殊需求;同时也需要注意避免编写过度复杂导致无法预料结果rules.UDEVD虽然稳健但错误配置可能导致无法预料问题因此需谨慎处理相关配置工作.
411 16
|
XML 缓存 Linux
在Linux环境下解决Visual Studio Code字体显示异常和字体替换方法。
解决Linux下VS Code字体显示异常,需要对Linux字体渲染机制有所理解,并对VS Code的配置选项进行合理设置。替换字体时则要通过系统字体配置或VS Code设置来完成。通过上述方法,可以有效地解决字体显示问题,从而提升代码编辑的视觉体验。
1727 21
|
12月前
|
存储 Linux
Linux环境下删除大文件后磁盘空间未释放问题诊断流程。
以上诊断流程涉及Linux底层机制与高级管理技能结合之处,并需要管理员根据实际环境灵活调整诊断策略与解决方案。
820 8
|
安全 应用服务中间件 网络安全
在Linux环境部署Flask应用并启用SSL/TLS安全协议
至此,你的Flask应用应该能够通过安全的HTTPS协议提供服务了。记得定期更新SSL证书,Certbot可以帮你自动更新证书。可以设定cronjob以实现这一点。
912 10
|
Ubuntu 安全 Linux
Ubuntu 24.10 发行版登场:Linux 6.11 内核、GNOME 47 桌面环境
Ubuntu 24.10 还带来了 GNOME 47,增强了性能和稳定性,并引入了新功能。此版本的 Ubuntu 还默认在采用 Nvidia 显卡的硬件上切换到 Wayland,并在支持的硬件上默认使用开源的 Nvidia 560 内核模块。 另外需要注意的是,Ubuntu 24.10 是稳定版本,但作为非 LTS 版本,仅支持 9 个月。
|
安全 Linux 网络安全
Linux系统初步设置本地Git环境和生成SSH密钥的步骤。
现在您的Linux系统已经配置好了Git环境,并创建并添加了SSH密钥,可以安全地与远端仓库进行交互,无论是克隆、推送还是拉取操作。此过程确保了数据传输的安全并使版本控制流程更为顺畅。使用Git时应考虑定期更新并管理您的凭据,以确保安全性。
1581 0
|
Kubernetes Linux 网络安全
Rocky Linux 8.9配置Kubernetes集群详解,适用于CentOS环境
初始化成功后,记录下显示的 `kubeadm join`命令。
873 0