当前位置:网站首页>VS2017 connects to MYSQL
VS2017 connects to MYSQL
2022-07-31 06:01:00 【Lobster shell me in】
一、环境配置
第一步:
属性管理器—>属性—>C/C+±–>常规—>附加包含目录(添加mysql下的include文件夹的路径)
第二步:
链接器—>常规—>附加库目录,把mysql文件里的lib文件的路径添加进去
第三步:
链接器—>输入—>附加依赖项,将libmysql.lib写入
第四步:
把静态库libmysql.libCopy into the solutionx64下的debug文件目录下
因为有多个Debug If you are not sure which one to copy toDebug下,可以在多个Debug都复制过去
这步完成,The environment is configured,So how do we know if there is a problem?接下来我们来进行测试
二、连接测试
打开VS2017—>工具—>连接到数据库,Select in the data sourceMySQL Database
填写:数据库名、Username and password and database table name
点击测试连接,If the test connection is successful,我们就可以在VSAdd, delete, modify, check and other operations on the database
这是数据库
三、代码实现
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<Windows.h>
#include<WinSock.h>
#include<mysql.h>
using namespace std;
#pragma comment(lib,"libmysql.lib")
#pragma comment(lib,"wsock32.lib")
MYSQL *mysql = new MYSQL; //mysql连接
MYSQL_FIELD *fd; //字段列数组
char field[32][32]; //存字段名二维数组
MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
MYSQL_ROW column; //一个行数据的类型安全(type-safe)的表示,表示数据行的列
char query[150]; //查询语句
bool ConnectDatabase();
bool QueryDatabase1();
//bool QueryDatabase2();
int main() {
ConnectDatabase();
QueryDatabase1();
//QueryDatabase2();
system("pause");
return 0;
}
bool ConnectDatabase() {
//初始化mysql
mysql_init(mysql);
//返回false则连接失败,返回true则连接成功
if (!(mysql_real_connect(mysql, "localhost", "root", "root", "test", 3306, NULL, 0))) //中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
{
printf("Error connecting to database02:%s\n", mysql_error(mysql));
return false;
}
else {
printf("Connected...\n");
return true;
}
return true;
}
bool QueryDatabase1() {
sprintf_s(query, "select * from t_news"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
mysql_query(mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
//返回0 查询成功,返回1查询失败
if (mysql_query(mysql, query)) //执行SQL语句
{
printf("Query failed (%s)\n", mysql_error(mysql));
return false;
}
else {
printf("query success\n");
}
//获取结果集
if (!(res = mysql_store_result(mysql))) //获得sql语句结束后返回的结果集
{
printf("Couldn't get result from %s\n", mysql_error(mysql));
return false;
}
//打印数据行数
printf("number of dataline returned: %lld\n", mysql_affected_rows(mysql));
//获取字段的信息
char *str_field[32]; //定义一个字符串数组存储字段信息
for (int i = 0; i < 6; i++) //在已知字段数量的情况下获取字段名
{
str_field[i] = mysql_fetch_field(res)->name;
}
for (int i = 0; i < 3; i++) //打印字段
printf("%10s\t", str_field[i]);
printf("\n");
//打印获取的数据
while (column = mysql_fetch_row(res)) //在已知字段数量情况下,获取并打印下一行
{
printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5]); //column是列数组
}
return true;
}
四、结果展示

五、扩展
5.1 每次新建项目,All need to reconfigure the database environment,There is a once and for all method,即:Create a project property sheet,之后的项目,Just add the configured table directly,No need to configure the environment every time.若有需要,可参考以下文章:
Visual Studio(VS)Save the project configuration to the property sheet
vs如何将工程配置,保存到属性表
5.2 mysql和vtkEqual library environments can be configured together in a property sheet
欢迎大家交流探讨!
边栏推荐
猜你喜欢

小米手机短信定位服务激活失败

Principle analysis of famous website msdn.itellyou.cn
![[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]](/img/3d/1b65fb33bfbf22da8243df1ab33ed5.png)
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]

unicloud 发布后小程序提示连接本地调试服务失败,请检查客户端是否和主机在同一局域网下

Eternal blue bug reappears

GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?

为什么bash中的read要配合while才能读取/dev/stdin的内容

动态规划(一)| 斐波那契数列和归递
![[Cloud native] Simple introduction and use of microservice Nacos](/img/06/b0594208d5b0cbf3ae8edd80ec12c4.png)
[Cloud native] Simple introduction and use of microservice Nacos

Year-end summary - the years are quiet~
随机推荐
Artifact SSMwar exploded Error deploying artifact.See server log for details
The server time zone value ‘й‘ is unrecognized or represents more than one time zone
【uiautomation】微信好友列表获取(存储到txt中)
[windows]--- SQL Server 2008 super detailed installation tutorial
sqlmap injection tutorial common commands
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
MySQL压缩包方式安装,傻瓜式教学
Android软件安全与逆向分析阅读笔记
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
Chinese garbled solution in UTF-8 environment in Powershell
cocos2d-x-3.2图片灰化效果
MySql to create data tables
CMOS管原理,及其在推挽电路中的应用
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
MySQL高级SQL语句(二)
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
cocos2d-x-3.2创建项目方法
Common JVM interview questions and answers
kotlin 插件更新到1.3.21