一、创建数据库
创建数据库db_myqq 创建用户信息表tb_account 创建部门表tb_department;
二、工程配置
VS版本:VS2019 mysql版本:
2.1 新建工程
工程随便选择,MFC、空工程都可以
2.2 复制文件夹
找到以下路径中的include和lib 将其放到新建的工程内部
2.3 属性配置
包涵引用目录include和库目录lib 附加依赖项:libmysql.lib
2.4 添加dll文件
将lib文件夹下的libmysql.dll文件放在程序运行目录下(debug/release) 若未放此文件,则程序启动会报缺少libmysql.dll文件。
三、程序
3.1 新建main.cpp文件
3.2 main.cpp
#include <Windows.h>
#include <mysql.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
const char user[] = "root";
const char pswd[] = "你的密码";
const char host[] = "127.0.0.1";
const char table[] = "db_myqq";
unsigned int port = 3306;
MYSQL myCont;
MYSQL_RES* result=nullptr;
MYSQL_ROW sql_row;
int res;
mysql_init(&myCont);
if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0))
{
mysql_query(&myCont, "SET NAMES GBK");
res = mysql_query(&myCont, "select * from tb_account");
if (!res)
{
result = mysql_store_result(&myCont);
if (result)
{
while (sql_row = mysql_fetch_row(result))
{
cout << " id:" << sql_row[0] << endl;
cout << " name:" << sql_row[1] << endl;
cout << " password:" << sql_row[2] << endl;
cout << " sex:" << sql_row[3] << endl;
cout << " age:" << sql_row[4] << endl;
cout << " department_id:" << sql_row[5] << endl;
cout << " department_name:" << sql_row[6] << endl;
cout << " headship:" << sql_row[7] << endl;
cout << " office_phone:" << sql_row[8] << endl;
cout << " hand_set:" << sql_row[9] << endl;
cout << " email:" << sql_row[10] << endl;
cout << " address:" << sql_row[11] << endl;
cout << " picture:" << sql_row[12] << endl;
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
mysql_free_result(result);
mysql_close(&myCont);
system("pause");
return 0;
}
四、运行
五、说明
位数需要保证和mysql一致
|