当前位置:网站首页>MySQL与c语言连接(vs2019版)
MySQL与c语言连接(vs2019版)
2022-07-06 09:14:00 【%xiao Q】
1. 选择相应的Relase X64 运行程序
2. 把MySQL的lib文件夹下的libmysql.dll和libmysql.lib复制到工程文件夹下
路径:C:\Download\Mysql\mysql-8.0.26-winx64\lib
复制到改路径下:D:\班级文件夹\MYSQL课设\职业介绍信息管理系统\职业介绍信息管理系统
3. 添加libmysql.lib到附加依赖项:属性 -> 链接器 -> 输入 -> 附加依赖项
4. 添加include到附加包含目录:属性 -> c/c++ -> 常规 -> 附加包含目录
include 路径:C:\Download\Mysql\mysql-8.0.26-winx64\include
MySQL初始化和变量的定义
// 包含的头文件
#include <stdio.h>
#include <WinSock.h>
#include <mysql.h>
MYSQL m; // mysql连接
MYSQL_RES* res; // 查询结果集
MYSQL_ROW row; //二维数组,存放数据
//初始化
mysql_init(&m);
// 设置编码方式
mysql_options(&m, MYSQL_SET_CHARSET_NAME, "gbk");
数据库的连接
// “主机”,“用户名”,“密码”,“数据库名”,“端口”
if (mysql_real_connect(&m, "localhost", "root", "0915", "test", 3306, NULL, 0))
printf("数据库连接成功\n");
else
printf("数据库连接失败 %s\n", mysql_error(&m)); // 输出错误信息
向数据库插入数据
// 定义一条sql语句
const char* sql = "insert into stu values('1001', '小明', 100)";
// mysql_query(&m, sql) 执行一条sql语句,成功返回0,失败返回非0
if (mysql_query(&m, sql))
printf("插入数据失败 %s\n", mysql_error(&m));
else
printf("插入数据成功\n");
// 向数据库插入多条数据
const char* sql = "insert into stu values('1002','小红', 98), ('1005', 'baby', 89), ('1004', 'asd', 95)";
if (mysql_query(&m, sql))
printf("插入数据失败:%s", mysql_error);
else printf("插入成功");
删除数据
const char* sql = "delete from stu where id = '1005'";
if (mysql_query(&m, sql))
printf("删除失败:%s", mysql_errno);
else
printf("删除成功\n");
更新数据
const char* sql = "update stu set id = '1003' where name = 'asd'";
if (mysql_query(&m, sql))
printf("更新失败:%s", mysql_error);
else
printf("更新成功\n");
查询数据
// 查询数据
const char* sql = "select * from stu";
if (mysql_query(&m, sql))
printf("未查到:%s", mysql_error);
else
printf("查找成功\n");
// 获取查询结果集
res = mysql_store_result(&m);
if (res)
printf("获取到数据\n");
else
printf("未获取到数据:%s", mysql_error);
// 打印数据
while (row = mysql_fetch_row(res))
{
printf("%s\t%s\t%s\t\n", row[0], row[1], row[2]);
}
//释放结果集
mysql_free_result(res); // 释放结果集
mysql_close(&m); //关闭数据库
下面在附一份源码
#include <stdio.h>
#include <WinSock.h>
#include <mysql.h>
void test()
{
MYSQL m; // mysql连接
MYSQL_RES* res; // 查询结果集
MYSQL_ROW row; //二维数组,存放数据
//初始化
mysql_init(&m);
// 设置编码方式
mysql_options(&m, MYSQL_SET_CHARSET_NAME, "gbk");
//连接数据库
// “主机”,“用户名”,“密码”,“数据库名”,“端口”
if (mysql_real_connect(&m, "localhost", "root", "0915", "test", 3306, NULL, 0))
printf("数据库连接成功\n");
else
printf("数据库连接失败 %s\n", mysql_error(&m)); // 输出错误信息
// 向数据库插入数据
const char* sql = "insert into stu values('1001', '小明', 100)";
if (mysql_query(&m, sql))
printf("插入数据失败 %s\n", mysql_error(&m));
else
printf("插入数据成功\n");
// 向数据库插入多条数据
const char* sql = "insert into stu values('1002','小红', 98), ('1005', 'baby', 89), ('1004', 'asd', 95)";
if (mysql_query(&m, sql))
printf("插入数据失败:%s", mysql_error);
else printf("插入成功");
//删除数据
const char* sql = "delete from stu where id = '1005'";
if (mysql_query(&m, sql))
printf("删除失败:%s", mysql_errno);
else
printf("删除成功\n");
//更新数据
const char* sql = "update stu set id = '1003' where name = 'asd'";
if (mysql_query(&m, sql))
printf("更新失败:%s", mysql_error);
else
printf("更新成功\n");
// 查询数据
//string s = "select * from stu";
//const *sql = s;
char sql[] = "select * from stu";
if (mysql_query(&m, sql))
printf("未查到:%s", mysql_error);
else
printf("查找成功\n");
// 获取查询结果集
res = mysql_store_result(&m);
if (res)
printf("获取到数据\n");
else
printf("未获取到数据:%s", mysql_error);
// 打印数据
while (row = mysql_fetch_row(res))
{
printf("%s\t%s\t%s\t\n", row[0], row[1], row[2]);
}
//释放结果集
mysql_free_result(res); // 释放结果集
mysql_close(&m); //关闭数据库
}
int main()
{
//cout << "main" << endl;
test();
getchar();
return 0;
}
边栏推荐
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- 学习问题1:127.0.0.1拒绝了我们的访问
- FRP intranet penetration
- When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
- neo4j安装教程
- Data dictionary in C #
- Invalid global search in idea/pychar, etc. (win10)
- Remember a company interview question: merge ordered arrays
- [蓝桥杯2017初赛]方格分割
- TCP/IP协议(UDP)
猜你喜欢
[recommended by bloggers] C # generate a good-looking QR code (with source code)
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
Neo4j installation tutorial
Basic use of redis
Request object and response object analysis
Did you forget to register or load this tag
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
02-项目实战之后台员工信息管理
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Rhcsa certification exam exercise (configured on the first host)
随机推荐
Picture coloring project - deoldify
Did you forget to register or load this tag 报错解决方法
Learning question 1:127.0.0.1 refused our visit
Project practice - background employee information management (add, delete, modify, check, login and exit)
AcWing 1294.樱花 题解
Redis的基础使用
double转int精度丢失问题
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Database advanced learning notes -- SQL statement
Some notes of MySQL
Solution: log4j:warn please initialize the log4j system properly
QT creator custom build process
[蓝桥杯2020初赛] 平面切分
LeetCode #461 汉明距离
【博主推荐】C# Winform定时发送邮箱(附源码)
01项目需求分析 (点餐系统)
Ansible实战系列二 _ Playbook入门
软件测试与质量学习笔记3--白盒测试
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Django running error: error loading mysqldb module solution