当前位置:网站首页>Insert data into MySQL in C language
Insert data into MySQL in C language
2022-07-30 17:11:00 【star and dream star_dream】
写在前面的话:
- The content of this chapter refers to the article:c语言 mysql数据库添加数据
https://blog.csdn.net/qq_32162235/article/details/107413823?ops_request_misc=&request_id=&biz_id=102&utm_term=c%E8%AF%AD%E8%A8%80%E5%B0%86%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5mysql%E4%B8%AD&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-107413823.142^v35^new_blog_pos_by_title&spm=1018.2226.3001.4187
- The code in this chapter can be run,能够添加到MySQL中(截图证明)
- This time the code runs in the console(采用VS的编译器)
#include<stdio.h>
#include<mysql.h>//包含mysql头文件
typedef struct Fruit
{
int id;
char f_name[20];
double f_price;
char f_create_time[40];
char f_update_time[40];
char f_content[255];
}Fruit;
//初始化信息
Fruit addInfomation(int id,char f_name[20],double f_price,char f_create_time[40],char f_update_time[40],char f_content[255]){
Fruit fruit = {id,f_name,f_price,f_create_time,f_update_time,f_content};
return fruit;
}
int main()
{
//定义句柄
MYSQL mysql;
//定义结构体
Fruit fruit;
//初始化句柄
if(NULL == mysql_init(&mysql))
{
printf("MySQL初始化失败!\n");
return -1;
}
//输入你的MySQLAccount and password and database name
if(NULL == mysql_real_connect(&mysql,"localhost","root","root","_db4",0,NULL,0))
{
//打印错误信息
printf("%s\n", mysql_error(&mysql));
return -1;
}
printf("连接数据库成功!\n");
//设置字符集
mysql_set_character_set(&mysql,"utf8");
//添加信息
Fruit fruit = addInfomation(5,"哈密瓜",24.3,"2022-12-08 12:03:33","2022-12-08 12:03:33","非常好吃");
char sql[1024];
//拼接SQL语句
sprintf(sql, "insert into student values(%d,'%s',%lf,'%s','%s','%s')",fruit.id,fruit.f_name,fruit.f_price,
fruit.f_create_time,fruit.f_update_time,fruit.f_content);
if(0 != mysql_query(&mysql,sql))
{
//打印信息
printf("%s\n",mysql_error(&mysql));
return -1;
}
printf("添加成功!\n");
mysql_close(&mysql);
return 0;
}
遇到的问题:
mysql.c(2): fatal error C1083: 无法打开包括文件: “mysql.h”: No such file or directory
解决方案:(添加进环境变量中)
- 首先找到mysql.h头文件:C:\Program Files\MySQL\MySQL Server 8.0\include
- 添加进环境变量
注意:当然,You probably don't have this system environment variable,你可以参考我之前写的文章
Compile the source file again,You can see that we compiled successfully! (The next step is to debug the code)
遇到的问题:
mysql.obj : error LNK2019: 无法解析的外部符号 mysql_init,函数 main 中引用了该符号
解决方案:
- just nowmysql.hThe header files are added,Now put it in the source file iemysql.c加进来
- The same operation as before,加进环境变量
库文件路径:C:\Program Files\MySQL\MySQL Server 8.0\lib
![]()
加进了环境变量,There are also include library files
#pragma comment(lib,"libmysql.lib")
再一次编译C语言源文件(成功!)
运行该可执行文件
遇到这种错误,就去我的电脑Find the change file in (Recommended software here:Everything)
我的文件目录:C:\Program Files\MySQL\MySQL Server 8.0\lib
完整代码(可运行,If you want to add to your own database适当修改一下)
#include<stdio.h>
#include<mysql.h>//包含mysql头文件
#pragma comment(lib,"libmysql.lib")
typedef struct Fruit
{
int id;
char f_name[20];
double f_price;
char f_create_time[40];
char f_update_time[40];
char f_content[255];
}Fruit;
int main()
{
//定义句柄
MYSQL mysql;
char sql[1024];
//初始化句柄
if(NULL == mysql_init(&mysql))
{
printf("MySQL初始化失败!\n");
return -1;
}
//输入你的MySQLAccount and password and database name
if(NULL == mysql_real_connect(&mysql,"localhost","root","root","_db4",3306,NULL,0))
{
//打印错误信息
printf("%s\n", mysql_error(&mysql));
return -1;
}
printf("连接数据库成功!\n");
//设置字符集
mysql_set_character_set(&mysql,"utf8");
//添加信息
Fruit fruit = {5,"apple",24.3,"2022-12-08","2022-12-08","very-good"};
//拼接SQL语句
sprintf(sql, "insert into fruits values(%d,'%s',%lf,'%s','%s','%s')",fruit.id,fruit.f_name,fruit.f_price,
fruit.f_create_time,fruit.f_update_time,fruit.f_content);
if(0 != mysql_query(&mysql,sql))
{
//打印信息
printf("%s\n",mysql_error(&mysql));
return -1;
}
printf("添加成功!\n");
mysql_close(&mysql);
return 0;
}
边栏推荐
- olap——入门ClickHouse
- [NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
- Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
- Oracle动态监听与静态监听详解
- Invalid or corrupt jarfile xxx.jar
- DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
- 基于stm32的shell实现
- bert-base调试心得
- Promise入门到精通(1.5w字详解)
- torch.optim.Adam() function usage
猜你喜欢
随机推荐
Mongoose module
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
镜像站收集
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
MySQL详细学习教程(建议收藏)
S7-200SMART中定时器的使用方法和常见注意事项汇总
LeetCode167:有序数组两数之和
onenote use
LeetCode167: Sum of two numbers in sorted array
优酷视频元素内容召回系统:多级多模态引擎探索
实现web实时消息推送的7种方案
微信小程序picker滚动选择器使用详解
lotus 1.16.0 最小快照导出 导入
云厂商做生态需要“真连接、真赋能”,用“技术+真金实银”发展伙伴
SLIM: Sparse Linear Methods (TopN推荐)
线程同步 控制执行顺序
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
Lotus 1.16.0 minimum snapshot export import
HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
获得抖音商品详情 API