1.连接MySQL数据库
(1)在MySQL安装路径中找到 mysql.h 这个文件,复制文件路径;
C:\Program Files\MySQL\MySQL Server 8.0\include
(2)打开项目——右键解决方案——点击属性——点击VC++目录——将mysql.h文件路径添加到包含目录中去;(记得在路径末尾加上分号:";")
(3)在刚刚找到的mysql.h目录的上一级目录中,找到lib目录,进入lib目录,复制路径
C:\Program Files\MySQL\MySQL Server 8.0\lib
(4)在解决方案的属性页 VC++目录 中的 库目录 中添加lib文件路径(同样在路径末尾加上分号";")
(5)进入lib目录,找到 libmysql.dll 文件,将该文件复制,粘贴到 C:\Windows\System32 目录下
(6)回到VS,继续打开解决方案的属性页——找到链接器——进入输入——附加依赖项——将libmysql.lib添加到依赖项中(末尾加上分号";")
2.编写源代码
源代码如下:
(1)Sql类:
#pragma once
#include<iostream>
#include<iomanip>
#include<mysql.h>
using namespace std;
class Sql{
private:
MYSQL *conn; //定义MySQL对象,连接器
MYSQL_RES* res; //定义MySQL结果集对象
MYSQL_ROW row; //定义MySQL行对象
public:
void Sqlopen()
{
conn = mysql_init(NULL); //初始化MySQL对象
mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gbk"); //设置字符编码
//连接数据库
//数据库指针对象,主机名,数据库用户名,密码,数据库名,端口号,UNIX套接字文件路径,客户端选项
if (mysql_real_connect(conn, "localhost", "root", "root", "student_info", 3306, NULL, 0) == NULL)
{
cout << "数据库连接失败,错误原因:" << mysql_error(conn) << std::endl;
exit(-1);
}
else
cout << "数据库连接成功啦!!!" << std::endl;
}
void Sqlclose()
{
//关闭数据库连接
mysql_close(conn);
}
void select()
{
if (mysql_query(conn, "SELECT * FROM `学生信息`")) //mysql_query()函数执行成功返回0,失败返回1;
{
//执行SELECT语句失败,返回错误信息
std::cerr<<"查询失败: Error: " << mysql_error(conn) << endl;
return;
}
res = mysql_use_result(conn);//获取结果集
cout << "学生信息:" << endl;
//输出表头
cout << left << "学号" << "\t\t" << "姓名" << "\t\t性别" << "\t\t出生日期" << "\t\t手机号" << endl;
//循环输出表中每一行数据
while ((row = mysql_fetch_row(res)) != NULL) {//获取一行数据,返回一个数字索引的数组
cout<< left<< row[0] << "\t" << row[1] << "\t\t" << row[2] << "\t\t" << row[3] << "\t\t" << row[4] << endl;
}
mysql_free_result(res);//释放结果集
}
void tianjia()
{
string str,shuxing[5];
cout << "请输入您需要添加的信息:" << endl << "学号:";
cin >> shuxing[0];
cout << endl << "姓名:";
cin >> shuxing[1];
cout << endl << "性别:";
cin >> shuxing[2];
cout << endl << "出生日期(xxxx-xx-xx):";
cin >> shuxing[3];
cout << endl << "联系电话:";
cin >> shuxing[4];
string query = "INSERT INTO 学生信息 (学号,姓名,性别,出生日期,联系方式) VALUES ('" + shuxing[0] + "', '" + shuxing[1] + "', '" + shuxing[2] + "', '" + shuxing[3] + "', '" + shuxing[4] + "')";
if (mysql_query(conn, query.c_str()))
std::cerr << "添加信息失败: Error: " << mysql_error(conn) << endl;
}
void xiugai()
{
string num;
cout << "请输入学生学号:";
cin >> num;
string query = "SELECT * FROM `学生信息` WHERE `学号` = " + num;
if (mysql_query(conn, query.c_str()) || mysql_fetch_row(res) == NULL)
{
std::cerr << "查询失败,未找到该学生信息: Error: " << mysql_error(conn) << endl;
return;
}
res = mysql_use_result(conn);//获取结果集
cout << "学生信息:" << endl;
//输出表头
cout << left << "学号" << "\t\t" << "姓名" << "\t\t性别" << "\t\t出生日期" << "\t\t手机号" << endl;
row = mysql_fetch_row(res);//获取一行数据,返回一个数字索引的数组;
cout << left << row[0] << "\t" << row[1] << "\t\t" << row[2] << "\t\t" << row[3] << "\t\t" << row[4] << endl;
mysql_free_result(res);//释放结果集
string str, shuxing[4];
cout << "请输入您需要修改的信息:";
cout << endl << "姓名:";
cin >> shuxing[0];
cout << endl << "性别:";
cin >> shuxing[1];
cout << endl << "出生日期(xxxx-xx-xx):";
cin >> shuxing[2];
cout << endl << "联系电话:";
cin >> shuxing[3];
string query1 = "UPDATE `学生信息` SET `姓名` = '" + shuxing[0] + "', `性别`='" + shuxing[1] + "', `出生日期`='" + shuxing[2] + "', `联系方式`='" + shuxing[3] + "' WHERE `学号` = " + num;
if(mysql_query(conn, query1.c_str()))
std::cerr << "修改信息失败: Error: " << mysql_error(conn) << endl;
}
void shanchu()
{
string num;
cout << "请输入学生学号:";
cin >> num;
string query = "SELECT * FROM `学生信息` WHERE `学号` = " + num;
if (mysql_query(conn, query.c_str()) || mysql_fetch_row(res) == NULL)
{
//执行SELECT语句失败,返回错误信息
std::cerr << "查询失败,未找到该学生信息: Error: " << mysql_error(conn) << endl;
return;
}
res = mysql_use_result(conn);//获取结果集
cout << "学生信息:" << endl;
//输出表头
cout << left << "学号" << "\t\t" << "姓名" << "\t\t性别" << "\t\t出生日期" << "\t\t手机号" << endl;
//循环输出表中每一行数据
row = mysql_fetch_row(res);//获取一行数据,返回一个数字索引的数组
cout << left << row[0] << "\t" << row[1] << "\t\t" << row[2] << "\t\t" << row[3] << "\t\t" << row[4] << endl;
int boo;
cout << endl << "是否删除这条学生信息?" << endl << "1.确认删除" << endl << "0.取消删除" << endl;
cin >> boo;
if (boo == 1)
{
string querysc = "DELETE FROM `学生信息` WHERE `学号` = " + num;
if(mysql_query(conn, querysc.c_str()))
std::cerr << "删除失败: Error: " << mysql_error(conn) << endl;
}
else if (boo == 0)
return;
else {
cout << endl << "您输入的有误!" << endl << "按任意键返回删除界面" << endl;
system("pause");
shanchu();
}
mysql_free_result(res);//释放结果集
}
};
(2)界面类:
#include<iostream>
#include "Sql.cpp"
using namespace std;
class Sql;
class 界面
{
private:
Sql sql;
public:
inline void menu();
inline void liulan();
inline void tianjia();
inline void xiugai();
inline void shanchu();
};
void 界面::menu()
{
while (true)
{
cout << "欢迎来到学生信息管理系统" << endl;
cout << "1.浏览学生信息" << endl;
cout << "2.添加学生信息" << endl;
cout << "3.修改学生信息" << endl;
cout << "4.删除学生信息" << endl;
cout << "5.退出" << endl;
cout << endl << "请选择您需要的功能:";
int n;
cin >> n;
switch (n)
{
case 1:
{
fflush(stdout); // 刷新缓存
sql.Sqlopen();
sql.select();
sql.Sqlclose();
cout << endl << "按任意键回到主菜单" << endl;
system("pause");
system("cls");
menu();
}
case 2:
{
sql.Sqlopen();
sql.tianjia();
sql.Sqlclose();
cout << endl << "按任意键回到主菜单" << endl;
system("pause");
system("cls");
menu();
}
case 3:
{
sql.Sqlopen();
sql.xiugai();
sql.Sqlclose();
cout << endl << "按任意键回到主菜单" << endl;
system("pause");
system("cls");
menu();
}
case 4:
{
sql.Sqlopen();
sql.shanchu();
sql.Sqlclose();
cout << endl << "按任意键回到主菜单" << endl;
system("pause");
system("cls");
menu();
}
case 5:
break;
default:
{
cout << endl << "您的输入有误,请重新输入" << endl;
system("pause");
system("cls");
menu();
}
}
break;
}
}
(3)主函数
// C++数据库开发_学生信息管理系统.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<mysql.h>
#include"界面.cpp"
#include"Sql.cpp"
using namespace std;
int main()
{
fflush(stdout); // 刷新缓存
界面 *jm = new 界面();
jm->menu();
delete jm;
fflush(stdout); // 刷新缓存
}
3.MySQL数据库设计,如下: