当前位置:网站首页>Vs how to read data in MySQL (by the way, the problem of Chinese garbled code is solved through code)
Vs how to read data in MySQL (by the way, the problem of Chinese garbled code is solved through code)
2022-07-26 20:18:00 【Pinot orange】
PS: I also read others' blogs , Then it took a long time , Feel troublesome to write this blog .
Catalog
One 、 stay vs Build a new project in , Then create a new source file
Two 、 Right click mysql_test project name , Then find the attribute at the bottom , And click the
Nine 、 If successful, the following prompt message box will be displayed
One 、 stay vs Build a new project in , Then create a new source file

Two 、 Right click mysql_test project name , Then find the attribute at the bottom , And click the

3、 ... and 、 Click on VC++ Catalog , Add... To the include directory mysql File path , In general mysql The default installation path of is C Discoid Program Files Under the , find include route , Then click OK

Four 、 stay C/C++ General options in , Find the additional containing Directory , At the same time mysql Of include Include the path


5、 ... and 、 Select the general options of the linker in the property page , Find the additional library directory inside , Then add mysql Of lib route

6、 ... and 、 In the input options of the linker , Find additional dependencies , Then write directly libmysql.lib

7、 ... and 、 Finally, mysql Of bin Directory , take libmysql.dll Make a copy to c:\windows\system32 Just go down the path .
8、 ... and 、 Just find a piece of code to test , Here is the code in others' blogs , Of course, some of them have been modified , Here will be the corresponding introduction .
mysql The test code is as follows :
create database test;
use test;
create table test_1(name varchar(128),age int);
insert into test_1 values(' Pinot orange ',18);vs The test code is as follows ( Pay attention to the modification mysql The user name and password are your own , As well as database And the name of the table inside , My user name here is root, The password is 123456,database It's the top mysql Test the test, Table name is test_1):
#include <stdio.h>
#include <WinSock.h>
#include <Windows.h>
#include <mysql.h>
#include<iostream>
using namespace std;
MYSQL mysql; //mysql Connect
MYSQL_RES* res; // A result aggregation construct
MYSQL_ROW row; //char** Two dimensional array , Keep records
const char DataBase_UserName[] = "root"; // Database user name username
const char DataBase_Password[] = "123456"; // Database password , Fill in your own password
const char DataBase_Host[] = "localhost"; // Database connection address
// Note that sometimes the host is used ip Will report a mistake , Now you can put ip Change it to localhost
const char DataBase_Name[] = "test"; //database name
unsigned int DataBase_Port = 3306; //server port
bool ConnectDatabase(); // Function declaration
void FreeConnect(); // Release resources
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(); // Connect to database
// Query data
mysql_query(&mysql, "SELECT * from test_1");
// Get the result set
res = mysql_store_result(&mysql);
// Display the data
// to ROW assignment , Judge ROW Is it empty , Print data if it is not empty .
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()
{
// initialization mysql
mysql_init(&mysql); // Connect mysql, database
// In the middle are the hosts , user name , password , Database name , Port number ( You can write the default 0 perhaps 3306 etc. ), You can write parameters first and then pass them in
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, " Connect MYSQL Data are successfully !", " news ", MB_OK);
printf("Connected...\n");
return true;
}
}
// Release resources
void FreeConnect() {
mysql_close(&mysql);
}
Nine 、 If successful, the following prompt message box will be displayed

Ten 、 Print the results ( Successful show )

边栏推荐
- LeetCode_回溯_中等_216.组合总和 III
- 靠元宇宙和NFT,天下秀疯狂“割韭菜”?
- Jincang database kingbasees SQL language reference manual (18. SQL statement: drop materialized view to drop synonym)
- UE5编辑器Slate快速入门【开篇】
- C # use the default transformation method
- Record an analysis of a.Net property management background service stuck
- C asynchronous programming read this article is enough
- 会议OA之会议排座&送审
- MySQL之InnoDB引擎(五)
- cv2.resize()
猜你喜欢

Record an analysis of a.Net property management background service stuck

一文看懂中国的金融体系

plsql包

There is an Oolong incident in open source. Maybe you don't know these five open source protocols

使用请求头认证来测试需要授权的 API 接口

网红辣条抓不住年轻人

一文读懂 .NET 中的高性能队列 Channel

高瓴加入的PRI,君联、华控、盛世、绿动等百家机构都杀进去了

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

These 22 drawing (visualization) methods are very important and worth collecting!
随机推荐
SQL injection
vs如何读取mysql中的数据(顺便通过代码解决了中文乱码问题)
C#将PDF文件转成图片
解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
福建争抢VC/PE
mysql使用union 排序问题
Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
Kingbasees SQL language reference manual of Jincang database (17. SQL statement: discard to drop language)
cv2.resize()
小公司小而美的产品,如何突围?
几张图帮你捋清“中国金融机构体系”
Student‘s t分布
C# 使用默认转型方法
Strengthen supervision on secret room escape and script killing, and focus on strengthening fire safety and juvenile protection
开源闹出乌龙事件,可能你不知道这五种开源协议
【JVM 系列】JVM 调优
Definition and use of one-dimensional array
URL format
C # convert PDF files into pictures
Dio问题总结