当前位置:网站首页>vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
2022-07-26 19:22:00 【獜洛橙】
PS:也是看了别人博客,然后也花了挺久的,觉得麻烦就写这篇博客。
目录
二、右击上述图片中的mysql_test工程名,然后在最下方找到属性,并点击
三、点击VC++目录,在包含目录中添加mysql的文件路径,一般情况下的mysql的默认安装路径都是在C盘的Program Files下的,找到include路径,然后点击确定
四、在C/C++中的常规选项中,找到附加包含目录,同时也将mysql的include路径包含进去
五、在属性页中选择链接器的常规选项,在里面找到附加库目录,然后添加mysql的lib路径
六、 在链接器的输入选项中,找到附加依赖项,然后直接写上libmysql.lib
七、最后再mysql的bin目录中,将libmysql.dll复制一份到c:\windows\system32路径下即可。
八、随便找一段代码测试,这里用的是其他人的博客里面的代码,当然有些许是修改了的,这里会作相应介绍。
一、在vs中新建一个工程,然后新建一个源文件

二、右击上述图片中的mysql_test工程名,然后在最下方找到属性,并点击

三、点击VC++目录,在包含目录中添加mysql的文件路径,一般情况下的mysql的默认安装路径都是在C盘的Program Files下的,找到include路径,然后点击确定

四、在C/C++中的常规选项中,找到附加包含目录,同时也将mysql的include路径包含进去


五、在属性页中选择链接器的常规选项,在里面找到附加库目录,然后添加mysql的lib路径

六、 在链接器的输入选项中,找到附加依赖项,然后直接写上libmysql.lib

七、最后再mysql的bin目录中,将libmysql.dll复制一份到c:\windows\system32路径下即可。
八、随便找一段代码测试,这里用的是其他人的博客里面的代码,当然有些许是修改了的,这里会作相应介绍。
mysql测试代码如下:
create database test;
use test;
create table test_1(name varchar(128),age int);
insert into test_1 values('獜洛橙',18);vs测试代码如下(要注意修改mysql用户名和密码为自己的,同时还有database的名字和里面的表的名字,我这里用户名是root,密码是123456,database是上面mysql测试代码中的test,表名是test_1):
#include <stdio.h>
#include <WinSock.h>
#include <Windows.h>
#include <mysql.h>
#include<iostream>
using namespace std;
MYSQL mysql; //mysql连接
MYSQL_RES* res; //一个结果集结构体
MYSQL_ROW row; //char** 二维数组,存放一条条记录
const char DataBase_UserName[] = "root"; //数据库用户名username
const char DataBase_Password[] = "123456"; //数据库密码,填自己的密码
const char DataBase_Host[] = "localhost"; //数据库连接地址
//注意有时候使用主机ip会报错,此时可以将ip改为localhost
const char DataBase_Name[] = "test"; //database name
unsigned int DataBase_Port = 3306; //server port
bool ConnectDatabase(); //函数申明
void FreeConnect(); //释放资源
wchar_t* Utf8_2_Unicode(char* row_i)
{
int len = MultiByteToWideChar(CP_UTF8, 0, row_i, strlen(row_i), NULL, 0);
wchar_t* wszStr = new wchar_t[len + 1];
MultiByteToWideChar(CP_UTF8, 0, row_i, strlen(row_i), wszStr, len);
wszStr[len] = '\0';
return wszStr;
}
void main()
{
setlocale(LC_ALL, "chs");
ConnectDatabase(); //连接数据库
//查询数据
mysql_query(&mysql, "SELECT * from test_1");
//获取结果集
res = mysql_store_result(&mysql);
//显示数据
//给ROW赋值,判断ROW是否为空,不为空就打印数据。
while (row = mysql_fetch_row(res))
{
wchar_t* m1 = Utf8_2_Unicode(row[0]);
wchar_t* m2 = Utf8_2_Unicode(row[1]);
wprintf_s(L"%s %s", m1,m2);
}
FreeConnect();
getchar();
}
bool ConnectDatabase()
{
//初始化mysql
mysql_init(&mysql); //连接mysql,数据库
//中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
if (!(mysql_real_connect(&mysql, DataBase_Host, DataBase_UserName, DataBase_Password, DataBase_Name, DataBase_Port, NULL, 0))) {
printf("Error connecting to database:%s\n", mysql_error(&mysql));
return false;
}
else {
MessageBoxA(NULL, "连接MYSQL数据成功!", "消息", MB_OK);
printf("Connected...\n");
return true;
}
}
//释放资源
void FreeConnect() {
mysql_close(&mysql);
}
九、如果成功即显示如下的提示信息框

十、打印结果(成功显示)

边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (13. SQL语句:ALTER SYNONYM 到 COMMENT)
- 猎聘问卷星,成为微信「寄生虫」
- 【MySQL】 - 索引原理与使用
- 一文看懂中国的金融体系
- 【Pytorch基础】torch.stack()函数解析
- Impersonate authentication
- Zhongtian steel uses tdengine in GPS and AIS scheduling
- go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
- Overview of canvas
- BluePrism流程业务对象的组件功能介绍-RPA第三章
猜你喜欢

How to adjust the abnormal win11 USB drive to normal?

Excel-VBA 快速上手(十、提示框、可输入的弹出框)

猎聘问卷星,成为微信「寄生虫」

What should we do about the fragmentation of internal information? Try this

Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

安全测试初学体验

An open source web drawing board is really convenient

Household deposits increased by 10.33 trillion yuan in the first half of the year, with an average of 57.1 billion deposits pouring into banks every day

URL格式

Where can I find the files downloaded from iPad
随机推荐
金仓数据库 KingbaseES SQL 语言参考手册 (16. SQL语句: CREATE SEQUENCE 到 DELETE)
svn使用碎碎念
Design of intelligent weighing system based on Huawei cloud IOT (STM32) [i]
Latte dht-phev products are very popular. Will the sales volume make Li Ruifeng figure it out?
Kingbasees SQL language reference manual of Jincang database (20. SQL statements: merge to values)
Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
Kingbases SQL language reference manual of Jincang database (15. SQL statement: create materialized view to create schema)
What should we do about the fragmentation of internal information? Try this
Read the high-performance queue channel in.Net
计算机专业面试题目总结,总导航
go+mysql+redis+vue3简单聊室,第6弹:使用vue3和element-plus调用接口
Zabbix调用api检索方法
LeetCode_回溯_中等_40.组合总和 II
2022年下半年(软考高级)信息系统项目管理师报名条件
Codeforces Round #810 (Div. 2)(A~C)
tf.GraphKeys
ES6的方法&类数组转成真正的数组&判断数组的方法
超强接口协作平台如何打造:细数Apifox的六把武器
如何实现一个能处理海量数据的异步任务队列系统(至尊典藏版)
金仓数据库 KingbaseES SQL 语言参考手册 (12. SQL语句:ALTER LANGUAGE 到 ALTER SUBSCRIPTION)