当前位置:网站首页>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
欢迎大家交流探讨!
边栏推荐
- (Crypto essential dry goods) Detailed analysis of the current NFT trading markets
- Android软件安全与逆向分析阅读笔记
- quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
- [Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
- quick-3.5 lua调用c++
- [Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
- mysql password modification method in Linux (pro-test available)
- 数据库 | SQL查询进阶语法
- 2021 Mianjing - Embrace Change
- 浅谈对分布式模式下CAP的理解
猜你喜欢
Install mysqldb in mac10.14
[Cloud native] Open source data analysis SPL easily copes with T+0
Common JVM interview questions and answers
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
MySQL压缩包方式安装,傻瓜式教学
What is an EVM Compatible Chain?
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
【uiautomation】微信好友列表获取(存储到txt中)
Chinese garbled solution in UTF-8 environment in Powershell
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
随机推荐
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
What is GameFi?
this points to the problem
SSH automatic reconnection script
Android软件安全与逆向分析阅读笔记
[uiautomation] Get WeChat friend list (stored in txt)
MySQL-如何分库分表?一看就懂
小米手机短信定位服务激活失败
【uiautomation】微信好友列表获取(存储到txt中)
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
Common JVM interview questions and answers
Fragmented NFT (Fractional NFT)
flutter arr dependencies
sql 添加 default 约束
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
VS通过ODBC连接MYSQL(一)
On the side of Ali, tell me what are the application scenarios of message middleware you know?
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
podspec自动化升级脚本