当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Rhcsa certification exam exercise (configured on the first host)
AcWing 242. A simple integer problem (tree array + difference)
How to configure flymcu (STM32 serial port download software) is shown in super detail
Django运行报错:Error loading MySQLdb module解决方法
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
Introduction and use of automatic machine learning framework (flaml, H2O)
引入了junit为什么还是用不了@Test注解
Generate PDM file from Navicat export table
CSDN markdown editor
One click extraction of tables in PDF
随机推荐
Principes JDBC
02-项目实战之后台员工信息管理
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
Solution: log4j:warn please initialize the log4j system properly
MySQL other hosts cannot connect to the local database
Ansible实战系列一 _ 入门
Asp access Shaoxing tourism graduation design website
Did you forget to register or load this tag 报错解决方法
JDBC原理
L2-001 紧急救援 (25 分)
One click extraction of tables in PDF
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
Redis的基础使用
csdn-Markdown编辑器
Django running error: error loading mysqldb module solution
Record a problem of raspberry pie DNS resolution failure
Some problems in the development of unity3d upgraded 2020 VR
误删Path变量解决
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Django运行报错:Error loading MySQLdb module解决方法