将VS环境配置好。并且做小测试,具体可见上篇文章https://blog.csdn.net/wjl990316fddwjl/article/details/135185173?spm=1001.2014.3001.5501
#define _CRT_SECURE_NO_WARNINGS
MYSQL *con;
const char* host = "127.0.0.1";
const char* user = "root";
const char* password = "*******";//数据库密码
const char* database_name = "database_test";
const int port = 3306;
#define _CRT_SECUER_NO_WARNINGS
#pragma once
class StudentManager
{
StudentManager();
~StudentManager();
};
//构造函数
StudentManager::StudentManager()
{
con = mysql_init(NULL);
mysql_library_init(0, NULL, NULL);
//这两行代码必须在上面
mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");//必须有的,具体看我前几篇文章
mysql_real_connect(con, host, user, password, database_name, port, NULL, 0);
}
//析构函数
StudentManager::~StudentManager()
{
mysql_close(con);
mysql_library_end();
}
这里说一个小技巧,右键构造函数或者析构函数,点击快速操作和重构—创建声明或定义,直接去StudentManager.cpp进行构造函数的扩充,这样就防止忘记了具体如何引用。
typedef struct STUDENT
{
int Student_id;//学生id;
string Student_name;//学生姓名
string Class_id;//班级id
}Student;
public:
bool insert_Student(Student& stu);//增加数据
bool delete_Student(int Student_id);//删除数据
bool update_Student(Student& stu);//修改数据
vectorget_Student_all(string condition = "");//查找数据,将所有的数据都输出,用容器
vectorget_Student_Single(Student& stu, string Student_name);//查找数据,根据姓名查找单个数据
INSERT into student(表名) values (学号,姓名,班级)
bool StudentManager::insert_Student(Student& stu)
{
char sql[1024];
sprintf(sql, "insert into student (student_id,student_name,class_id) values (%d,'%s','%s')",
stu.Student_id, stu.Student_name.c_str(), stu.Class_id.c_str());
//判断是否添加成功
if (mysql_query(con, sql))
{
fprintf(stderr, "添加失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
bool StudentManager::delete_Student(int Student_id)
{
char sql[1024];
sprintf(sql, "delete from student where student_id=%d\n", Student_id);
if (mysql_query(con, sql))
{
fprintf(stderr, "删除失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
bool StudentManager::update_Student(Student& stu)
{
char sql[1024];
sprintf(sql, "update student set student_name='%s',class_id='%s' where student_id=%d\n",
stu.Student_name.c_str(), stu.Class_id.c_str(), stu.Student_id);
if (mysql_query(con, sql))
{
fprintf(stderr, "更新失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
vector StudentManager::get_Student_all(string condition)
{
vectorstulist;//创建stulist容器,待会将数据存放在里面
char sql[1024];
sprintf(sql, "select * from student %s\n", condition.c_str());
if (mysql_query(con, sql))
{
fprintf(stderr, "查找失败:error:%s\n", mysql_error(con));
return {};//敲黑板 这里不可以是bool了,vector应该是{}
}
//获取查找到的数据
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while (row=mysql_fetch_row(res))
{
Student stu;
//atoi将string转换为int
stu.Student_id = atoi(row[0]);
stu.Student_name = row[1];
stu.Class_id = row[2];
//将得到的数据存在stulist里面
stulist.push_back(stu);
}
return stulist;
}
vector StudentManager::get_Student_Single(Student& stu, string Student_name)
{
char sql[1024];
vectorstulist;
sprintf(sql, "select * from student where student_name='%s'\n", stu.Student_name.c_str());
if (mysql_query(con, sql))
{
fprintf(stderr, "查找失败:error:%s\n", mysql_error(con));
return {};
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while (row=mysql_fetch_row(res))
{
Student stu;
stu.Student_id = atoi(row[0]);
stu.Student_name = row[1];
stu.Class_id = row[2];
stulist.push_back(stu);
}
return stulist;
}
测试之前,设置单例模式,代码如下
public:
//设置一个返回 单例模式
static StudentManager* GetInstance()
{
//static静态变量全局只有一个,这样的话就不用创建多个连接了,只有一个连接就可以了
static StudentManager StudentManager;
return &StudentManager;
}
Student stu1 = { 444,"小王","测控一班" };
StudentManager::GetInstance()->insert_Student(stu1);
打开Navicat,看到数据已经插入进去了
Student stu2;
stu2.Student_id = 444;
StudentManager::GetInstance()->delete_Student(stu2.Student_id);
打开Navicat,看到数据已经删除了
Student stu3 = { 333,"小樊","电器三班" };
StudentManager::GetInstance()->update_Student(stu3);
打开Navicat,看到数据已经更新了
vectorret= StudentManager::GetInstance()->get_Student_all();
//容器遍历函数
for (vector::iterator it = ret.begin();it != ret.end();it++)
{
cout << "学生ID :" << (*it).Student_id << "\n"
<< "学生姓名:" << (*it).Student_name << "\n"
<< "学生班级:" << (*it).Class_id << "\n"
<< endl;
}
运行
Student stu;
stu.Student_name = "小樊";
vectorret1 = StudentManager::GetInstance()->get_Student_Single(stu, stu.Student_name);
for (vector::iterator it = ret1.begin();it != ret1.end();it++)
{
cout << "学生ID :" << (*it).Student_id << "\n"
<< "学生姓名:" << (*it).Student_name << "\n"
<< "学生班级:" << (*it).Class_id << "\n"
<< endl;
}
运行
#define _CRT_SECUER_NO_WARNINGS
#pragma once
#include
#include
#include
#include
using namespace std;
typedef struct STUDENT
{
int Student_id;//学生id;
string Student_name;//学生姓名
string Class_id;//班级id
}Student;
class StudentManager
{
StudentManager();
~StudentManager();
public:
bool insert_Student(Student& stu);//增加数据
bool delete_Student(int Student_id);//删除数据
bool update_Student(Student& stu);//修改数据
vectorget_Student_all(string condition = "");//查找数据,将所有的数据都输出,用容器
vectorget_Student_Single(Student& stu, string Student_name);//查找数据,根据姓名查找单个数据
public:
//设置一个返回 单例模式
static StudentManager* GetInstance()
{
//static静态变量全局只有一个,这样的话就不用创建多个连接了,只有一个连接就可以了
static StudentManager StudentManager;
return &StudentManager;
}
private:
MYSQL* con;
const char* host = "127.0.0.1";
const char* user = "root";
const char* password = "990107Wjl@";
const char* database_name = "database_test";
const int port = 3306;
};
#define _CRT_SECURE_NO_WARNINGS
#include "StudentManager.h"
StudentManager::StudentManager()
{
con = mysql_init(NULL);
mysql_library_init(0, NULL, NULL);
//这两行代码必须在上面
mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");//必须有的,具体看我前几篇文章
mysql_real_connect(con, host, user, password, database_name, port, NULL, 0);
}
StudentManager::~StudentManager()
{
mysql_close(con);
mysql_library_end();
}
bool StudentManager::insert_Student(Student& stu)
{
char sql[1024];
sprintf(sql, "insert into student (student_id,student_name,class_id) values (%d,'%s','%s')",
stu.Student_id, stu.Student_name.c_str(), stu.Class_id.c_str());
//判断是否添加成功
if (mysql_query(con, sql))
{
fprintf(stderr, "添加失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
bool StudentManager::delete_Student(int Student_id)
{
char sql[1024];
sprintf(sql, "delete from student where student_id=%d\n", Student_id);
if (mysql_query(con, sql))
{
fprintf(stderr, "删除失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
bool StudentManager::update_Student(Student& stu)
{
char sql[1024];
sprintf(sql, "update student set student_name='%s',class_id='%s' where student_id=%d\n",
stu.Student_name.c_str(), stu.Class_id.c_str(), stu.Student_id);
if (mysql_query(con, sql))
{
fprintf(stderr, "更新失败:error:%s\n", mysql_error(con));
return false;
}
return true;
}
vector StudentManager::get_Student_all(string condition)
{
vectorstulist;//创建stulist容器,待会将数据存放在里面
char sql[1024];
sprintf(sql, "select * from student %s\n", condition.c_str());
if (mysql_query(con, sql))
{
fprintf(stderr, "查找失败:error:%s\n", mysql_error(con));
return {};//敲黑板 这里不可以是bool了,vector应该是{}
}
//获取查找到的数据
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while (row=mysql_fetch_row(res))
{
Student stu;
//atoi将string转换为int
stu.Student_id = atoi(row[0]);
stu.Student_name = row[1];
stu.Class_id = row[2];
//将得到的数据存在stulist里面
stulist.push_back(stu);
}
return stulist;
}
vector StudentManager::get_Student_Single(Student& stu, string Student_name)
{
char sql[1024];
vectorstulist;
sprintf(sql, "select * from student where student_name='%s'\n", stu.Student_name.c_str());
if (mysql_query(con, sql))
{
fprintf(stderr, "查找失败:error:%s\n", mysql_error(con));
return {};
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while (row=mysql_fetch_row(res))
{
Student stu;
stu.Student_id = atoi(row[0]);
stu.Student_name = row[1];
stu.Class_id = row[2];
stulist.push_back(stu);
}
return stulist;
}
#define _CRT_SECURE_NO_WARNINGS
#include"StudentManager.h"
int main()
{
/*Student stu1 = { 444,"小王","测控一班" };
StudentManager::GetInstance()->insert_Student(stu1);*/
/*Student stu2;
stu2.Student_id = 444;
StudentManager::GetInstance()->delete_Student(stu2.Student_id);*/
/*Student stu3 = { 333,"小樊","电器三班" };
StudentManager::GetInstance()->update_Student(stu3);*/
/*vectorret= StudentManager::GetInstance()->get_Student_all();
for (vector::iterator it = ret.begin();it != ret.end();it++)
{
cout << "学生ID :" << (*it).Student_id << "\n"
<< "学生姓名:" << (*it).Student_name << "\n"
<< "学生班级:" << (*it).Class_id << "\n"
<< endl;
}*/
Student stu;
stu.Student_name = "小樊";
vectorret1 = StudentManager::GetInstance()->get_Student_Single(stu, stu.Student_name);
for (vector::iterator it = ret1.begin();it != ret1.end();it++)
{
cout << "学生ID :" << (*it).Student_id << "\n"
<< "学生姓名:" << (*it).Student_name << "\n"
<< "学生班级:" << (*it).Class_id << "\n"
<< endl;
}
system("pause");
return 0;
}