基于MySQL数据库的高校人员信息管理系统

大一下课程设计的项目,也算人生第一次的大项目了哈哈,写这篇文章记录一下。

#include<iostream>
#include<string>
#include<mysql.h>
#include<stdlib.h>
#include<limits>;
#include<sstream>
#include<iomanip>
using namespace std;
const char* host = "127.0.0.1";
const char* user = "root";
const char* pw = "li000000";
const char* database_name = "manager";
const int port = 3306;
class University_staff
{
public:
	int ID;
	string Name, Gender;
	int Age;
	virtual void Add_newdata_to_database(MYSQL*con)
	{
		string sql = "INSERT INTO university_staff(ID, Name, Gender, Age) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ")";
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con)<<endl;
			return;
		}
	}
	University_staff(int id, string name, string gender, int age) :ID(id), Name(name), Gender(gender), Age(age)
	{

	}
	virtual void print()
	{
		cout <<setw(10)<<left<< "oridinary staff:" << ID<<" " << Name<<" " << Gender<<" " << Age << endl;
	}
};

class Laboratory:public University_staff
{
	
public:
	static int Kind;
	string Lab_name, Position;
	void print()
	{
		cout << setw(18) << left;
		cout<< "Laboratory:" << setw(10) << left << ID << setw(10) << left << Name << setw(4) << left
			<< Gender << setw(4) << left << Age << setw(14) << left << Lab_name << setw(14) << left << Position << endl;
	};
	Laboratory(int id,string name,string gender,int age,string labname,string position):University_staff(id,name,gender,age),
		Lab_name(labname),Position(position)
	{
	}
	virtual void Add_newdata_to_database(MYSQL* con)
	{

		string sqla = "INSERT INTO university_staff(ID, Name, Gender, Age) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ")";
		if (mysql_query(con, sqla.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
		string str = "INSERT INTO laboratory(ID,Name,Gender,Age,Lab_name,Position)VALUES(" + to_string(ID) + ",'" 
			+ Name + "','"+ Gender+"',"+to_string(Age)+",'"+Lab_name+"','"+Position+"')";
		if (mysql_query(con, str.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
			return;
		}
	}
};
int Laboratory::Kind = 0;

class Teacher :virtual public University_staff
{
public:
	static int Kind;
	string Major, Section, Title;
	void print()
	{
		cout << setw(18) << left;
		cout<< "Teacher:" << setw(10) << left << ID << setw(10) << left<<Name<<setw(4)<<left
			<< Gender << setw(4) << left << Age << setw(14) << left << Major << setw(14) << left
			<< Section << setw(12) << left << Title << endl;
	}
	Teacher(int id, string name, string gender, int age, string major, string section, string title) :University_staff(id, name, gender, age),
		Major(major), Section(section), Title(title)
	{

	}
	virtual void Add_newdata_to_database(MYSQL*con)
	{
		string sqla = "INSERT INTO university_staff(ID, Name, Gender, Age) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ")";
		if (mysql_query(con, sqla.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
		string sql = "INSERT INTO teacher(ID,Name,Gender,Age,Major,Section,Title)VALUES(" + to_string(ID)
			+ ",'" + Name + "','" + Gender + "'," + to_string(Age) + ",'" + Major + "','" + Section + "','" + Title + "')";
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
	}
};
int Teacher::Kind = 1;

class Official :virtual public University_staff
{
public:
	static int Kind;
	string Politics_status,Position;
	void print()
	{
		cout<<setw(18)<<left<<"Official:"<<setw(10) << left << ID << setw(10) << left 
			<< Name <<setw(4) << left << Gender << setw(4) << left<<
			Age << setw(14) << left << Politics_status << setw(14) << left << Position << endl;
	}
	Official(int id, string name, string gender, int age, string status,string position) :University_staff(id, name, gender, age),
		Politics_status(status),Position(position)
	{

	}
	void Add_newdata_to_database(MYSQL*con)
	{
		string sqla = "INSERT INTO university_staff(ID, Name, Gender, Age) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ")";
		if (mysql_query(con, sqla.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
		string sql = "INSERT INTO official(ID, Name, Gender, Age,Politics_status,Position) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ",'"+Politics_status+"','"+Position+"')";
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
	}
};
int Official::Kind = 2;

class Official_Teacher : public University_staff
{
public:
	string Politics_status, Position,Major, Section, Title;
	static int Kind;
	void print()
	{
		cout << setw(18) << left <<"Official teacher:" << setw(10) << left <<ID << setw(10) << left
			<< Name << setw(4) << left << Gender << setw(4) << left << Age
			<< setw(14) << left << Major << setw(14) << left << Section
			<< setw(12) << left << Title << setw(12) << left << Politics_status << setw(12) << left << Position<< endl;
	}
	Official_Teacher(int id, string name, string gender, int age, 
		string major, string section, string title,string status, string position ) :
		University_staff(id, name, gender, age), Politics_status(status), Position(position), Major(major), Section(section), Title(title)
	{

	}
	void Add_newdata_to_database(MYSQL* con)
	{
		string sqla = "INSERT INTO university_staff(ID, Name, Gender, Age) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ")";
		if (mysql_query(con, sqla.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}

		string sql = "INSERT INTO official_teacher(ID, Name, Gender, Age,politics_status, Position,Major, Section, Title) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ",'"+Politics_status+"','"+Position+"','"+Major+"','"+Section+"','"+Title+"')";
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			return;
		}
		/*string sqlb = "INSERT INTO official(ID, Name, Gender, Age,Politics_status,Position) "
			"VALUES (" + to_string(ID) + ", '"
			+ Name + "', '"
			+ Gender + "', "
			+ to_string(Age) + ",'" + Politics_status + "','" + Position + "')";
		if (mysql_query(con, sqlb.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			exit(-1);
		}
		string sqlc = "INSERT INTO teacher(ID,Name,Gender,Age,Major,Section,Title)VALUES(" + to_string(ID)
			+ ",'" + Name + "','" + Gender + "'," + to_string(Age) + ",'" + Major + "','" + Section + "','" + Title + "')";
		if (mysql_query(con, sqlc.c_str()))
		{
			cerr << "INSERT ERROR:" << mysql_error(con) << endl;
			exit(-1);
		}
		*/
	}
};
int Official_Teacher::Kind = 3;
class Node
{
	
public:
	University_staff* data;
	Node* next;
	Node()
	{
		data = nullptr;
		next = nullptr;
	}
	Node(University_staff* x)
	{
		data = x;
		next = nullptr;
	}
};
class Managerment
{
	
public:
	static string secret;
	static bool ismanager;
	static Node* head;
	static Node* tail;
	static void show_message()
	{
		Node* t = head;
		while (t != nullptr&&t->data!=nullptr)
		{
			t->data->print();
			if (t == tail)
			{
				return;
			}
			t = t->next;
			
		}
	}
	static void fetch_data_from_databases(MYSQL* con)
	{
	
		void fetch_teacher(MYSQL * con);
		void fetch_official_teacher(MYSQL * con);
		void fetch_official(MYSQL * con);
		void fetch_laboratory(MYSQL * con);
		fetch_laboratory(con);
		fetch_teacher(con);
		fetch_official(con);
		fetch_official_teacher(con);
		
	}
	static bool is_repeat_id(int id)
	{
		
		Node* t = head;
		while (t != nullptr&&t->data!=nullptr)
		{
			if (t->data->ID == id)
			{
				return true;
			}
			if (t == tail)return false;
			t = t->next;
		}
		return false;
	}
	static bool is_repeat_name(string name)
	{

		Node* t = head;
		while (t != nullptr)
		{
			if (t->data->Name == name)
			{
				return true;
			}
			t = t->next;
		}
		return false;
	}
	static void Add_new_data(int kind)//插入到链表里面
	{

		switch (kind)
		{
		case 0:
		{
			cout << "请逐项输入该laboratory的ID,name,gender,age,lab_name, position:" << endl;
			int id, age;
			string name, gender, lab_name, position,ju;
			cin>> id >> name >> gender >> age >> lab_name >> position;
			if (Managerment::is_repeat_id(id))
			{
				cout << "repeat id insert!" << endl;
				return;
			}
			Laboratory* p5 = new Laboratory(id,name,gender,age,lab_name,position);
			Node* newnode = new Node(p5);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
				cout << "添加成功!" << endl;
				return;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
			cout << "添加成功!" << endl;
			return;
		}
		case 1:
		{
			cout << "请逐项输入该teacher的ID,name,gender,age,major, section, title:" << endl;
			int id, age;
			string name, gender, major, section, title;
			cin >> id >> name >> gender >> age >> major >> section >> title;
			if (Managerment::is_repeat_id(id))
			{
				cout << "repeat id insert!" << endl;
				return;
			}
			Teacher* p1 = new Teacher(id,name,gender,age,major,section,title);
			Node* newnode = new Node(p1);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
			cout << "添加成功!" << endl;
			return;
		}
		case 2:
		{
			cout << "请逐项输入该official的ID,name,gender,age,politics_status, position:" << endl;
			int id, age;
			string name, gender, politics_status, position;
			cin >> id >> name >> gender >> age >> politics_status >> position;
			if (Managerment::is_repeat_id(id))
			{
				cout << "repeat id insert!" << endl;
				return;
			}
			Official* p2 = new Official(id,name, gender,age, politics_status, position);
			Node* newnode = new Node(p2);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
			cout << "添加成功!" << endl;
			return;
		}
		case 3:
		{
			cout << "请逐项输入该official teacher的ID,name,gender,age,major, section, title,politics_status, position:" << endl;
			int id, age;
			string name,gender, politics_status, position, major, section, title;
			cin >> id >> name >> gender >> age>> major >> section >> title >> politics_status >> position;
			if (Managerment::is_repeat_id(id))
			{
				cout << "repeat id insert!" << endl;
				return;
			}
			Official_Teacher* p3 = new Official_Teacher(id,name,gender,age,politics_status,position,major,section,title);
			Node* newnode = new Node(p3);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
			cout << "添加成功!" << endl;
			return ;
		}
		default :
		{
			cout << "This kind is no exist!" << endl;
			return;
		}
		}
	}
	static void Search_id(int id)//从链表里面搜索
	{
		Node* temp = head;
		while (temp != nullptr)
		{
			if (temp->data->ID == id)
			{
				cout << "Seatch successfully!" << endl;
				cout << "此人信息为:";
				temp->data->print();
				return;
			}
			if (temp == tail)
			{
				cout << "No exist!"<<endl;
				return;
			}
			temp = temp->next;
		}
		cout << "No exist!"<<endl;
	}
	static void Search_name(string n)//从链表里面搜索
	{
		Node* temp = head;
		while (temp != nullptr)
		{
			if (temp->data->Name == n)
			{
				cout << "Search successfully!" << endl;
				cout << "此人信息为:";
				temp->data->print();
				return;
			}
			if (temp == tail)
			{
				cout << "No exist!" << endl;
				return;
			}
			temp = temp->next;
		}
		cout << "No exist!";
	}
	static unsigned long Compile_num_by_kind(MYSQL* con, string tablename)//从数据库里面搜索
	{
		unsigned long rowCount = 0;
		string sql = "SELECT COUNT(*) FROM " +tablename;//表名前面要空格!!!!!!
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
			exit(-1);
		}
		MYSQL_RES* result = mysql_store_result(con);//获取结果集
		if (result != nullptr)
		{
			MYSQL_ROW row = mysql_fetch_row(result);//将result结果集里面的一行以数组形式赋到row中
			if (row != nullptr && row[0] != nullptr)
			{
				rowCount = stoul(row[0],NULL,10);
			}
			mysql_free_result(result);//释放结果集
		}
		return rowCount;
	}
	static unsigned long Compile_num_by_gender(MYSQL* con, string gender)//从链表里面搜索
	{
		unsigned long count = 0;
		Node* t = head;
		while (t != nullptr)
		{
			if (t->data->Gender == gender)
			{
				++count;
			}
			if (t== tail)
			{
				
				return count;
			}
			t = t->next;
		}
		return count;
	}
	static void Delete_data_by_id(int  id)//从链表里删除
	{
		if (Managerment::is_repeat_id(id))
		{
			Node* temp = head;
			Node* prep = nullptr;
			while (temp != nullptr)
			{
				if (head->data->ID == id)
				{
					Node* temp = head->next;
					delete head;
					head = new Node();
					head = temp;
					cout << "Delete successfully!" << endl;
					return;
				}
				if (temp->data->ID == id)
				{
					prep->next = temp->next;
					delete temp;
					temp = new Node();
					cout << "Delete successfully!" << endl;
					return;
				}
				prep = temp;
				temp = temp->next;
			}
		}
		else
		{
			cout << "No exist!" << endl;
		}
	}
	static void Delete_data_by_name(string name)//从链表里删除
	{
		if (Managerment::is_repeat_name(name))
		{
			Node* temp = head;
			Node* prep = nullptr;
			while (temp != nullptr)
			{
				if (head->data->Name == name)
				{
					Node* temp = head->next;
					delete head;
					head = new Node();
					head = temp;
					cout << "Delete successfully!" << endl;
					return;
				}
				if (temp->data->Name == name)
				{
					prep->next = temp->next;
					delete temp;
					temp = new Node();
					cout << "Delete successfully!" << endl;
					return;
				}
				prep = temp;
				temp = temp->next;
			}
		}
		else
		{
			cout << "No exist!" << endl;
		}
	}
	static void Edit_by_id(int id,int kind)//在链表中编辑
	{
		if (Managerment::is_repeat_id(id))
		{
			Node* temp = head;
			while (temp != nullptr)
			{
				if (temp->data->ID == id)
				{
					switch (kind)
					{
					case 0:
					{
						int id, age;
						string name, gender, lab_name, position;
						cout << "请逐项输入该laboratory的ID,name,gender,age,lab_name, position:" << endl;
						cin >> id >> name >> gender >> age >> lab_name >> position;
						Laboratory* p = new Laboratory(id, name, gender, age, lab_name, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 1:
					{
						int id, age;
						string name, gender, major, section, title;
						cout << "请逐项输入该teacher的ID,name,gender,age,major, section, title:" << endl;
						cin >> id >> name >> gender >> age >> major >> section >> title;
						Teacher* p = new Teacher(id, name, gender, age, major, section, title);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 2:
					{
						int id, age;
						string name, gender, politics_status, position;
						cout << "请逐项输入该official的ID,name,gender,age,politics_status, position:" << endl;
						cin >> id >> name >> gender >> age >> politics_status >> position;
						Official* p = new Official(id, name, gender, age, politics_status, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 3:
					{
						int id, age;
						string name, gender, major, section, title, politics_status, position;
						cout << "请逐项输入该official teacher的ID,name,gender,age,major, section, title,politics_status, position:" << endl;
						cin >> id >> name >> gender >> age >> major >> section >> title >> politics_status >> position;
						Official_Teacher* p = new Official_Teacher(id, name, gender, age, major, section,
							title, politics_status, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					default:
					{
						cout << "没有这个种类!" << endl;
					}
					}
				}
				temp = temp->next;
			}
		}
		else
		{
			cout << "No exist!" << endl;
		}
	}
	static void Edit_by_name(string name,int kind)//在链表中编辑
	{
		if (Managerment::is_repeat_name(name))
		{
			Node* temp = head;
			while (temp != nullptr)
			{
				if (temp->data->Name == name)
				{
					switch (kind)
					{
					case 0:
					{
						int id, age;
						string name, gender, lab_name, position;
						cout << "请逐项输入该laboratory的ID,name,gender,age,lab_name, position:" << endl;
						cin >> id >> name >> gender >> age >> lab_name >> position;
						Laboratory* p = new Laboratory(id, name, gender, age, lab_name, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 1:
					{
						int id, age;
						string name, gender, major, section, title;
						cout << "请逐项输入该teacher的ID,name,gender,age,major, section, title:" << endl;
						cin >> id >> name >> gender >> age >> major >> section >> title;
						Teacher* p = new Teacher(id, name, gender, age, major, section, title);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 2:
					{
						int id, age;
						string name, gender, politics_status, position;
						cout << "请逐项输入该official的ID,name,gender,age,politics_status, position:" << endl;
						cin >> id >> name >> gender >> age >> politics_status >> position;
						Official* p = new Official(id, name, gender, age, politics_status, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					case 3:
					{
						int id, age;
						string name, gender, major, section, title, politics_status, position;
						cout << "请逐项输入该official teacher的ID,name,gender,age,major, section, title,politics_status, position:" << endl;
						cin >> id >> name >> gender >> age >> major >> section >> title >> politics_status >> position;
						Official_Teacher* p = new Official_Teacher(id, name, gender, age, major, section,
							title, politics_status, position);
						temp->data = p;
						cout << "修改成功!" << endl;
						return;
					}
					}
				}
				temp = temp->next;
			}
		}
		else
		{
			cout << "No exist!" << endl;
		}
	}
	static void Save_data(MYSQL* con)//把链表的数据保存到数据库中
	{
		string sql = "TRUNCATE TABLE laboratory";
		string sql2= "TRUNCATE TABLE teacher";
		string sql3= "TRUNCATE TABLE official";
		string sql4= "TRUNCATE TABLE official_teacher";
		string sql5 = "TRUNCATE TABLE university_staff";
		if (mysql_query(con, sql.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
		}
		if (mysql_query(con, sql2.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
		}
		if (mysql_query(con, sql3.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
		}
		if (mysql_query(con, sql4.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
		}
		if (mysql_query(con, sql5.c_str()))
		{
			cerr << "ERROR:" << mysql_error(con) << endl;
		}
		Node* t = head;
		while (t != nullptr)
		{
			t->data->Add_newdata_to_database(con);
			if (t == tail)
			{
				return;
			}
			t = t->next;
		}
	}
};
Node*Managerment::head=nullptr;
Node* Managerment::tail = nullptr;
bool Managerment::ismanager= false;
string Managerment::secret = "123456li";
void fetch_laboratory(MYSQL* con)
{
	const char* sql5 = "SELECT ID,Name,Gender,Age,Lab_name,Position FROM laboratory";//sql查询语句
	if (mysql_query(con, sql5))
	{
		cerr << "ERROR:" << mysql_error(con) << endl;//检验sql语句是否有效
		return;
	}
	MYSQL_RES* result5 = mysql_store_result(con);
	if (result5 == nullptr)
	{
		cerr << "ERROR:" << mysql_error(con) << endl;
		return;
	}
	int num_rows5 = mysql_num_rows(result5);
	MYSQL_ROW row5;
	for (int i = 0; i < num_rows5; ++i)
	{
		row5 = mysql_fetch_row(result5);
		if (row5 != nullptr)
		{
			Laboratory* p5 = new Laboratory(stoi(row5[0]), row5[1], row5[2], stoi(row5[3]),
				row5[4], row5[5]);
			Node* newnode = new Node(p5);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
		}
	}
	mysql_free_result(result5);
}
void fetch_teacher(MYSQL* con)
{
	const char* sql2 = "SELECT ID,Name,Gender,Age,Major,Section,Title FROM Teacher";//sql查询语句
	if (mysql_query(con, sql2))
	{
		cerr << "ERROR:" << mysql_error(con) << endl;//检验sql语句是否有效
		return;
	}
	MYSQL_RES* result2 = mysql_store_result(con);
	if (result2 == nullptr)
	{
		cerr << "ERROR:" << mysql_error(con) << endl;
		return;
	}
	int num_rows2 = mysql_num_rows(result2);
	MYSQL_ROW row2;
	for (int i = 0; i < num_rows2; ++i)
	{
		row2 = mysql_fetch_row(result2);
		if (row2 != nullptr)
		{
			Teacher* p2 = new Teacher(stoi(row2[0]), row2[1], row2[2], stoi(row2[3]), row2[4], row2[5], row2[6]);
			Node* newnode = new Node(p2);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
		}
	}
	mysql_free_result(result2);
}
void fetch_official(MYSQL* con)
{
	const char* sql4 = "SELECT ID,Name,Gender,Age,Politics_status,Position FROM official";//sql查询语句
	if (mysql_query(con, sql4))
	{
		cerr << "ERROR:" << mysql_error(con) << endl;//检验sql语句是否有效
		return;
	}
	MYSQL_RES* result4 = mysql_store_result(con);
	if (result4 == nullptr)
	{
		cerr << "ERROR:" << mysql_error(con) << endl;
		return;
	}
	int num_rows4 = mysql_num_rows(result4);
	MYSQL_ROW row4;
	for (int i = 0; i < num_rows4; ++i)
	{
		row4 = mysql_fetch_row(result4);
		if (row4 != nullptr)
		{
			Official* p4 = new Official(stoi(row4[0]), row4[1], row4[2], stoi(row4[3]),
				row4[4], row4[5]);
			Node* newnode = new Node(p4);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
		}
	}
	mysql_free_result(result4);
}
void fetch_official_teacher(MYSQL* con)
{
	const char* sql3 = "SELECT ID,Name,Gender,Age,Major,Section,Title,Politics_status,Position FROM official_teacher";//sql查询语句
	if (mysql_query(con, sql3))
	{
		cerr << "ERROR:" << mysql_error(con) << endl;//检验sql语句是否有效
		return;
	}
	MYSQL_RES* result3 = mysql_store_result(con);
	if (result3 == nullptr)
	{
		cerr << "ERROR:" << mysql_error(con) << endl;
		return;
	}
	int num_rows3 = mysql_num_rows(result3);
	MYSQL_ROW row3;
	for (int i = 0; i < num_rows3; ++i)
	{
		row3 = mysql_fetch_row(result3);
		if (row3 != nullptr)
		{
			Official_Teacher* p3 = new Official_Teacher(stoi(row3[0]), row3[1], row3[2], stoi(row3[3]),
				row3[4], row3[5], row3[6], row3[7], row3[8]);
			Node* newnode = new Node(p3);
			if (Managerment::tail == nullptr)
			{
				Managerment::head = Managerment::tail = newnode;
			}
			Managerment::tail->next = newnode;
			Managerment::tail = Managerment::tail->next;
		}
	}
	mysql_free_result(result3);
}
bool inputsecret()
{
	cout << "你目前的权限为游客,请输入系统管理员密码:" << endl;
	string sec;
	cin >> sec;
	if (sec == Managerment::secret)
	{
		Managerment::ismanager = true;
		cout << "密码正确!" << endl;
		return true;
	}
	cout << "密码错误!" << endl;
	return false;
}
int main()
{
	
	MYSQL* con = mysql_init(NULL);
	mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");
	if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0))
	{
		cerr << "mysql connect error!";
		exit(-1);
	} 
	Managerment::fetch_data_from_databases(con);
	int choice = 0;
	for (int i = 0; i < 108; ++i)
		cout << "*";
	cout << endl;
	for (int i = 0; i < 35;++i)
		cout << " ";
	cout << "欢迎使用高校人员信息管理系统!" << endl
		<< "请输入你要选择的功能:" << endl
		<< "0(添加数据) " << "1(删除数据) " << "2(修改数据) " << "3(查询数据) " 
		<< "4(显示现有数据)" << "5(查询统计数据) " << "6(保存数据) "<<"7(退出系统)"<<endl;
	for (int i = 0; i < 108; ++i)
		cout << "*";
	cout << endl;
	while (cin >> choice)
	{
		
		switch (choice)
		{
		case 0:
		{
			if (!Managerment::ismanager)
			if (!inputsecret())
				continue;
				
			cout << "请输入你增加数据的数据类型: " << "0(laboratory) " << "1(teacher) " 
				<< "2(official) " << "3(official teacher)" << endl;
			int kind;
			cin >> kind;
			Managerment::Add_new_data(kind);
			break;
		}

		case 1:
		{
			if (!Managerment::ismanager)
				if (!inputsecret())
					continue;
			cout << "请输入你删除数据的方式" << endl
				<< "0(输入id) " << "1(输入名字)" << endl;
			int kind;
			cin >> kind;
			switch (kind)
			{
			case 0:
			{
				cout << "请输入你要删除数据的ID:" << endl;
				int id;
				cin >> id;
				Managerment::Delete_data_by_id(id);
				break;
			}
			case 1:
			{
				cout << "请输入你要删除数据的名字:" << endl;
				string name;
				cin >> name;
				Managerment::Delete_data_by_name(name);
				break;
			}
			default :
			{
				cout << "没有这个方式!" << endl;
			}
			}
			break;
		}

		case 2:
		{
			if (!Managerment::ismanager)
				if (!inputsecret())
					continue;
			cout << "请输入你修改数据的方式" << endl
				<< "0(输入id) " << "1(输入名字)" << endl;
			int kind;
			cin >> kind;
			switch (kind)
			{
			case 0:
			{
				cout << "请输入你要修改数据的ID:" << endl;
				int id;
				cin >> id;
				cout << "请输入新数据的类型:" << "0(laboratory) " << "1(teacher) "
					<< "2(official) " << "3(official teacher)" << endl;
				int variety;
				cin >> variety;
				Managerment::Edit_by_id(id, variety);
				break;
			}
			case 1:
			{
				cout << "请输入你要删除数据的名字:" << endl;
				string name;
				cin >> name;
				cout << "请输入新数据的类型:" << "0(laboratory) " << "1(teacher) "
					<< "2(official) " << "3(official teacher)" << endl;
				int variety;
				cin >> variety;
				Managerment::Edit_by_name(name, variety);
				break;
			}
			default:
			{
				cout << "没有这个方式!" << endl;
			}
			break;
			}
			break;
		}

		case 3:
		{
			cout << "请输入你搜索数据的方式" << endl
				<< "0(输入id) " << "1(输入名字)" << endl;
			int kind;
			cin >> kind;
			switch (kind)
			{
			case 0:
			{
				cout<<"请输入此人ID:"<< endl;
				int id;
				cin >> id;
				Managerment::Search_id(id);
				break;
			}
			case 1:
			{
				cout << "请输入此人姓名:" << endl;
				string name;
				cin >> name;
				Managerment::Search_name(name);
				break;
			}
			default:
			{
				cout << "没有这个方式!" << endl;
				break;
			}
			}
			break;
		}

		case 4:
		{
			Managerment::show_message();
			break;
		}

		case 5: 
		{
			Managerment::Save_data(con);
			cout << "请输入你想要查询的统计数据:" << endl
				<< "0(全体人员总数) 1(男职员总数) 2(女职员总数) 3(laboratory总数) 4(teacher总数) "
				<< "5(official总数) 6(official teacher总数)" << endl;
			int variety;
			cin >> variety;
			switch (variety)
			{
			case 0:
			{
				string gender1 = "男", gender2 = "女";
				unsigned int num1= Managerment::Compile_num_by_gender(con, gender1);
				unsigned int num2 = Managerment::Compile_num_by_gender(con, gender2);
				cout << "人员总数为:" << num1 + num2<<endl;
				break;
			}
			case 1:
			{
				string gender1 = "男";
				unsigned int num1 = Managerment::Compile_num_by_gender(con, gender1);
				cout << "人员总数为:" << num1<<endl;
				break;
			}
			case 2:
			{
				string  gender2 = "女";
				unsigned int num2 = Managerment::Compile_num_by_gender(con, gender2);
				cout << "人员总数为:" <<  num2 << endl;
				break;
			}
			case 3:
			{
				string tablename = "laboratory";
				unsigned int num = Managerment::Compile_num_by_kind(con,tablename);
				cout << "laboratory总数为:" << num << endl;
				break;
			}
			case 4:
			{
				string tablename = "teacher";
				unsigned int num = Managerment::Compile_num_by_kind(con, tablename);
				cout << "teacher总数为:" << num << endl;
				break;
			}
			case 5:
			{
				string tablename = "official";
				unsigned int num = Managerment::Compile_num_by_kind(con, tablename);
				cout << "official总数为:" << num << endl;
				break;
			}
			case 6:
			{
				string tablename = "official teacher";
				unsigned int num = Managerment::Compile_num_by_kind(con, tablename);
				cout << "official teacher总数为:" << num << endl;
				break;
			}
			default:
			{
				cout << "没有这个选项!" << endl;
				break;
			}
			}
			break;
		}

		case 6:
		{
			Managerment::Save_data(con);
			cout << "保存成功!" << endl;
			break;
		}

		case 7:
		{
			cout << "是否保存到数据库? 0(不保存) 1(保存)" << endl;
			int variety;
			cin >> variety;
			if (variety)
			{
				Managerment::Save_data(con);
				cout << "保存成功!" << endl;
				return 0;
			}
			else return 0;
			break;
		}

		default :
		{
			cout << "没有这个选项" << endl;
			break;
		}
		}
		for (int i = 0; i < 108; ++i)
			cout << "*";
		cout << endl;
		cout << "请输入你要选择的功能:" << endl
			<< "0(添加数据) " << "1(删除数据) " << "2(修改数据) " << "3(查询数据) "
			<< "4(显示现有数据) " << "5(统计数据) " << "6(保存数据) " << "7(退出系统)" << endl;
		for (int i = 0; i < 108; ++i)
			cout << "*";
		cout << endl;
	}
	mysql_close(con);
	return 0;
}

这个系统还是很简陋,日后学习更多知识以后可能会添加一些额外的功能吧。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值