当前位置:网站首页>在win10下使用visual studio2015链接mysql数据库
在win10下使用visual studio2015链接mysql数据库
2022-06-25 22:08:00 【smileapples】
环境介绍
操作系统:win10
mysql数据库版本(64位):
visual studio:2015专业版
安装步骤
0、安装mysql数据库(默认安装路径)
1、连接数据库首先配置环境
先将C:\Program Files\MySQL\MySQL Server 8.0\lib下的libmysql.dll复制到C:\Windows\System32中
2、修改win32 consoleapplication 为64位

如果不修改为x64,会报如下错误:
3、项目属性->VC+±>包含目录添加mysql\include路径,用来引用头文件

4、项目属性->链接器->常规->附加库目录 添加mysql\lib路径

5、项目属性->链接器->输入->附加依赖项 添加libmysql.lib名称即可(只添加静态库)

测试代码:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include<mysql.h>
#pragma comment(lib,"libmysql")
int main()
{
//初始化
MYSQL * mysql = mysql_init(NULL);
if (mysql == NULL)
{
printf("初始化失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
//改变字符集为utf-8
system("chcp 65001");
mysql_set_character_set(mysql, "utf8");
MYSQL * contMysql = mysql_real_connect(mysql,
"127.0.0.1",//地址
"root",//账号
"xxxxx",//密码
"testdb",//数据库
3306,//端口
NULL,
0
);
if (contMysql == NULL)
{
printf("链接失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
std::cout << "Connect success\n";
//创建数据库
int ret = mysql_query(contMysql, "create database If Not Exists myNewDatabase");
if (ret)
{
printf("创建数据库失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
//创建表
ret = mysql_query(contMysql, "create table If Not Exists myNewDatabase.T_STUDENT (sid varchar(50),sname varchar(50))");
if (ret){
printf("创建表失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
//增加数据
ret = mysql_query(contMysql, "create table If Not Exists myNewDatabase.T_STUDENT (sid varchar(50),sname varchar(50))");
if (ret){
printf("创建表失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
//插入
ret = mysql_query(contMysql, "insert into myNewDatabase.T_STUDENT values('s002','你好')");
if (ret){
printf("插入失败%s", mysql_error(mysql));
return EXIT_FAILURE;
}
//查询
ret = mysql_query(contMysql, "select * from myNewDatabase.T_STUDENT ");
if (ret){
printf("Query faliure %s\n", mysql_error(mysql));
return EXIT_FAILURE;
}
else {
//保存查询的结果
MYSQL_RES* res = mysql_store_result(contMysql);
MYSQL_FIELD* field;
//查询有哪些字段这里返回 sid sname
while ((field = mysql_fetch_field(res)))
{
printf(" %s\t", field->name);
}
unsigned int num_fields = mysql_num_fields(res);
printf("\r\n");
MYSQL_ROW row;
while (row = mysql_fetch_row(res))
{
for (size_t i = 0; i < num_fields; i++)
{
printf("%s ", row[i]);
}
printf("\r\n");
}
mysql_free_result(res);
}
mysql_close(contMysql);
return 0;
}
测试结果

边栏推荐
- Uniapp -- list page of multi header tabs
- 登录拦截器
- Qlabel text scrolling horizontally
- QT Chinese and English use different fonts respectively
- 对伪类的理解
- A. Balance the Bits--Codeforces Round #712 (Div. 1)
- Download the latest V80 version of Google Chrome
- Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
- MySQL InnoDB锁知识点
- Line height for small use
猜你喜欢

iomanip头文件在实战中的作用

STL教程5-STL基本概念及String和vector使用

提取系统apk

Binary, hexadecimal, big end and small end

Graduation trip | recommended 5-day trip to London

Database - mongodb

一文讲透研发效能!您关心的问题都在

使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容

idea Kotlin版本升级

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
随机推荐
C# IO Stream 流(二)扩展类_封装器
Stream in PHP socket communication_ Understanding of select method
Two ways to center block level elements
Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
对伪类的理解
Mutual conversion of float type data and datetime type data in sqlserver2008
xtrabackup的备份还原
7.常用指令(下)v-on,v-bind,v-model的常见操作
String object (constant) pool
Blob
6.常用指令(上)v-cloak,v-once,v-pre
Share a downloaded osgeo4w64 Library Based on qgis3.10
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
谈一谈生产环境中swoole协程创建数量控制机制
Style setting when there is a separator in the qcombobox drop-down menu
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
php中使用google protobuf协议环境配置
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
MySQL InnoDB锁知识点