mysql_free_result()函数的作用是释放结果集的内存,是同步的,也就是要中断一下
该实验使用了MySQL链接数据库的基本使用方法,具体使用了
MYSQL_RES 数据库的mysql_store_result()函数的返回值是一个结果集,该函数的作用是检索比并存储整个结果集
MYSQL_ROW 数据库的mysql_fetch_row()函数的返回值是一个集行,搭配上一个mysql_res使用的,一个一个读他,函数的作用是获取下一个结果集行
mysql_query()函数是执行语句的意思,也就是发给数据库让他执行这条语句,函数传递语句的使用如果是std::string 类型的话,需要调用.c_str()函数进行转化成字符才能进行传递
cpp
std::ifstream infile("../src/student2.txt",std::ios::in);
infile>>temp[j];
cpp
std::ofstream outfile("../src/student2.txt",std::ios::out);
outfile<<"学号\t"<<"名字\t"<<"得分\n"<<std::endl;
该方法分别对应了流对于文件的读入和写入操作的执行,使用完毕之后要执行close()操作
本实验我认为最重要的是对于mysql的链接建立问题
cpp
MYSQL mysql;
const char db_Username[]="root";
const char db_Password[]="123456";
const char db_Host[]="localhost";
const char db_Name[]="sp";
unsigned long db_Port=3306;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gbk");
if(mysql_real_connect(&mysql,db_Host,db_Username,db_Password,db_Name,db_Port,NULL,0)==NULL){
std::cout<<"connect error!!!"<<std::endl;
return false;
}
std::cout<<"connect success!!!"<<std::endl;
- build
- docs
- include
- Main.h
- Student.h
- src
- main.cpp
- Methods.cpp
- student.cpp
- student2.txt
- CMakeLists.txt
Main.h
cpp
#ifndef NEW_PROJECTS_MAIN_H
#define NEW_PROJECTS_MAIN_H
#include<iostream>
#include<mysql.h>
#include <fstream>
void show_mean();//显示菜单
bool connect_mysql(MYSQL& mysql);//链接数据库
void query(MYSQL& mysql);//查询学生数据
void modify(MYSQL& mysql);//修改学生数据
void insert(MYSQL& mysql);//添加学生数据
void Delete(MYSQL& mysql);//删除学生数据
void filein(MYSQL& mysql);//导入文件数据
int getline();//获取文件行数
void fileout(MYSQL& mysql);//将数据库中的文件导出到另外一个文件中
void createTable(MYSQL& mysql);//创建表格
#endif //NEW_PROJECTS_MAIN_H
Student.h
cpp
#ifndef NEW_PROJECTS_STUDENT_H
#define NEW_PROJECTS_STUDENT_H
#include"main.h"
class Student{
private:
int Id;
std::string name;
int score;
public:
int getId();
std::string getName();
int getScore();
void setId(int& Id);
void setName(std::string& name);
void setScore(int& score);
};
#endif //NEW_PROJECTS_STUDENT_H
main.cpp
cpp
#include "Main.h"
int main() {
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gbk");
system("chcp 65001");
if(connect_mysql(mysql)==false){
return 0;
}
else{
int choices=1;
while(choices!=0){
show_mean();
std::cin>>choices;
switch (choices) {
case 1:
filein(mysql);
break;
case 2:
query(mysql);
break;
case 3:
modify(mysql);
break;
case 4:
insert(mysql);
break;
case 5:
Delete(mysql);
break;
case 0:
fileout(mysql);
break;
default:
std::cout<<"Input error,please again!!!"<<std::endl;
break;
}
}
std::cout<<"success"<<std::endl;
system("pause");
system("cls");
}
}
Methods.cpp
cpp
#include"Main.h"
#include"Student.h"
void show_mean() // 展示功能菜单并让用户选择功能
{
std::cout << std::endl << "=========================================欢迎使用学生管理系统========================================" << std::endl << std::endl;;
std::cout << "请选择您要进行的操作" << std::endl;
std::cout << "\t 1. 读取学生数据并显示" << std::endl;
std::cout << "\t 2. 查询学生数据" << std::endl;
std::cout << "\t 3. 修改学生数据" << std::endl;
std::cout << "\t 4. 添加学生数据" << std::endl;
std::cout << "\t 5. 删除学生数据" << std::endl;
std::cout << "\t 0. 退出管理系统" << std::endl;
}
void create_table(MYSQL& mysql){
char Quest[]= "CREATE TABLE IF NOT EXISTS `Student1`(\
`Id` int AUTO_INCREMENT,\
`name` varchar(20),\
`score` double,\
PRIMARY KEY (`Id`)\
)ENGINE=InnoDB DEFAULT CHARSET=utf8;";
int re=mysql_query(&mysql,Quest);
if(re==0)
std::cout<<"table already"<<std::endl;
}
bool connect_mysql(MYSQL& mysql){
const char db_Username[]="root";
const char db_Password[]="123456";
const char db_Host[]="localhost";
const char db_Name[]="sp";
unsigned long db_Port=3306;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gbk");
if(mysql_real_connect(&mysql,db_Host,db_Username,db_Password,db_Name,db_Port,NULL,0)==NULL){
std::cout<<"connect error!!!"<<std::endl;
return false;
}
std::cout<<"connect success!!!"<<std::endl;
create_table(mysql);
return true;
}
int getline(){
int len=0;
std::ifstream infile("../src/student2.txt",std::ios::in);
char c;
// if (!infile.is_open()) {
// std::cerr << "Error: Unable to open file. ";
// std::cerr << "Error code: " << errno << std::endl;
// return -1; // 或者其他适当的错误码
// }
while(infile.get(c)){
// if(c=='\n')len++;
len++;
std::cout<<c;
}
infile.close();
std::cout<<"data_len is "<<len-1<<std::endl;
return len;
}
void filein(MYSQL& mysql){
int n=getline();
std::cout<<"already student data:"<<std::endl;
mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gbk");
std::ifstream infile("../src/student2.txt",std::ios::in);
const int N=300;
const int N2=100;
std::string temp[N];
Student* student=new Student[N2];
int x=0;
int tempId;
int tempScore;
if(!infile.is_open()){
std::cout<<"file open errr!!!"<<std::endl;
exit(0);
}
else{
char line[256]={0};
int i=0;
while(!infile.eof()&&i<N){
for(int j=i;j<i+3;j++){
infile>>temp[j];
std::cout<<"\t\t"<<temp[j]<<"\t";
}
std::cout<<std::endl;
tempId=atoi(temp[i].c_str());
tempScore=atoi(temp[i+2].c_str());
student[x].setId(tempId);
student[x].setName(temp[i+1]);
student[x].setScore(tempScore);
x++;
i=i+3;
}
infile.close();
}
std::cout<<"insert into student1 table"<<std::endl;
for(int i=1;i<x;i++){
std::string sql = "insert into Student1 values (" + std::to_string(student[i].getId()) + "," + "'" + student[i].getName() + "'" + "," + std::to_string(student[i].getScore()) + ")";
std::cout<<sql<<std::endl;
mysql_query(&mysql,sql.c_str());
}
delete[] student;
}
void fileout(MYSQL& mysql){
MYSQL_RES* res;
MYSQL_ROW row;
std::ofstream outfile("../src/student2.txt",std::ios::out);
mysql_query(&mysql,"select * from student1 order by Id");
res= mysql_store_result(&mysql);
std::cout<<"data:"<<std::endl;
std::cout<<"学号\t"<<"名字\t"<<"得分\n"<<std::endl;
outfile<<"学号\t"<<"名字\t"<<"得分\n"<<std::endl;
while(row= mysql_fetch_row(res)){
std::cout<<"\t"<<row[0]<<"\t\t"<<row[1]<<"\t\t"<<row[2]<<std::endl;
outfile << row[0] << "\t" << row[1] << "\t" << row[2] << std::endl;
}
std::cout << "他们将被保存到../src/student2.txt中" << std::endl;
outfile.close();
mysql_free_result(res);
}
void query(MYSQL& mysql) // 查询学生数据
{
MYSQL_RES* res; // 创建一个结果集
MYSQL_ROW row; // 二维数组存放记录
std::cout << "您选择了查询学生信息" << std::endl;
std::cout << "请输入您要查询的选项" << std::endl;
std::cout << "\t 1. 查询全部学生" << std::endl;
std::cout << "\t 2. 按学号查询" << std::endl;
std::cout << "\t 3. 按照名字进行查询" << std::endl;
std::cout << "\t 4. 按照分数进行查询" << std::endl;
std::cout << "\t 5.自定义SQL命令进行查询" << std::endl;
int choice;
std::string sql;
std::cin >> choice;
switch (choice) {
case 1:
{
std::cout << "全体学生信息如下:" << std::endl;
sql = "select * from student1 order by Id";
std::cout << "查询的SQL语句如下:" << std::endl << sql << std::endl; // 检查语句
mysql_query(&mysql, sql.c_str());
break;
}
case 2:
{
std::cout << "请输入要查询的学生学号" << std::endl;
int Id;
std::cin >> Id;
sql = "select * from student1 where Id = " + std::to_string(Id) + " order by Id";
std::cout << "查询的SQL语句如下:" << std::endl << sql << std::endl; // 检查语句
mysql_query(&mysql, sql.c_str());
break;
}
case 3:
{
std::cout << "请输入要查询的学生姓名" << std::endl;
std::string name;
std::cin >> name;
sql = "select * from student1 where name = '" + name + "' order by Id";
std::cout << "查询的SQL语句如下:" << std::endl << sql << std::endl; // 检查语句
mysql_query(&mysql, sql.c_str());
break;
}
case 4:
{
std::cout << "请输入要查询的学生分数" << std::endl;
int score;
std::cin >> score;
sql = "select * from student1 where score = " + std::to_string(score) + " order by Id";
std::cout << "查询的SQL语句如下:" << std::endl << sql << std::endl; // 检查语句
mysql_query(&mysql, sql.c_str());
break;
}
case 5:
{
std::cout << "请输入SQL命令语句" << std::endl;
std::cin.ignore(1024, '\n');
getline(std::cin, sql);
std::cout << "查询的SQL语句如下:" << std::endl<< sql << std::endl; // 检查语句
mysql_query(&mysql, sql.c_str());
break;
}
default:
std::cout << "输入有误请重试" << std::endl;
}
std::cout << "\t\t查询结果如下" << std::endl;
res = mysql_store_result(&mysql); // 将查询得到的数据存入结果集
if (res == NULL)
std::cout << "所有学生均与你查询的数据不匹配" << std::endl;
else {
std::cout << "\t学号\t" << "\t姓名\t" << "\t得分\t" << std::endl;
while (row = mysql_fetch_row(res))
{
std::cout << "\t" << row[0] << "\t\t" << row[1] << "\t\t" << row[2] << std::endl;
}
}
mysql_free_result(res);
}
void modify(MYSQL& mysql) // 修改学生数据
{
int choice;
int Id;
std::string name;
int score;
std::string sql1;
std::string sql2;
std::string sql;
MYSQL_RES* res;
MYSQL_ROW row;
std::cout << "您选择了修改学生信息" << std::endl;
std::cout << "请输入您要修改的学生的信息及数据" << std::endl;
std::cout << "\t 1. 按学号修改" << std::endl;
std::cout << "\t 2. 按名字修改" << std::endl;
std::cin >> choice;
switch (choice)
{
case 1:
{
std::cout << "请输入要修改的学生学号" << std::endl;
std::cin >> Id;
sql = "select * from student1 where Id = " + std::to_string(Id) + " order by Id";
// sql = "select * from student1 where Id =" + to_string(Id);
mysql_query(&mysql, sql.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row == NULL)
{
std::cout << "不存在该学号的学生" << std::endl;
break;
}
else
std::cout << "请输入修改之后的学生姓名" << std::endl;
std::cin >> name;
std::cout << "请输入修改之后的学生分数" << std::endl;
std::cin >> score;
sql1 = "update Student1 set name= '" + name + "' where Id=" + std::to_string(Id);
sql2 = "update Student1 set score= " + std::to_string(score) + " where Id=" + std::to_string(Id);
std::cout << "修改的的SQL语句如下:" << std::endl << sql1 << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql1.c_str());
mysql_query(&mysql, sql2.c_str());
break;
}
case 2:
{
std::cout << "请输入要修改的学生姓名" << std::endl;
std::cin >> name;
sql1 = "select * from Student1 where name ='" + name + "'";
mysql_query(&mysql, sql1.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row == NULL)
{
std::cout << "不存在该姓名的学生" << std::endl;
break;
}
else
std::cout << "请输入修改之后的学生学号" << std::endl;
std::cin >> Id;
std::cout << "请输入修改之后的学生分数" << std::endl;
std::cin >> score;
sql1 = "update Student1 set Id= " + std::to_string(Id) + " where name='" + name + "'";
sql2 = "update Student1 set score= " + std::to_string(score) + " where name='" + name + "'";
std::cout << "修改的的SQL语句如下:" << std::endl << sql1 << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql1.c_str());
mysql_query(&mysql, sql2.c_str());
break;
}
default:
std::cout << "输入有误请重试" << std::endl;
}
//mysql_free_result(res);
}
void insert(MYSQL& mysql)//添加学生信息
{
int Id;
std::string name;
int score;
MYSQL_RES* res; // 创建一个结果集
MYSQL_ROW row; // 二维数组存放记录
std::string sql1;
std::string sql2;
std::cout << "您选择了添加学生信息" << std::endl;
std::cout << "请输入学生学号" << std::endl;
std::cin >> Id;
sql1 = "select * from student1 where Id = " + std::to_string(Id) + " order by Id";
mysql_query(&mysql, sql1.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row != NULL)
std::cout << "已存在该学号的学生,系统不允许学号相同,请重新添加或选择修改该学生信息" << std::endl;
else {
std::cout << "请输入学生名字" << std::endl;
std::cin >> name;
std::cout << "请输入学生得分" << std::endl;
std::cin >> score;
sql2 = "insert into Student1 values (" + std::to_string(Id) + "," + "'" + name + "'" + "," + std::to_string(score) + ")";
std::cout << "插入的SQL语句如下:" << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql2.c_str());
// 使用sql命令对数据库进行数据添加
}
mysql_free_result(res);
}
void Delete(MYSQL& mysql) // 删除学生数据
{
int choice = 1;
int Id;
std::string name, sql1, sql2;
int score;
MYSQL_RES* res;
MYSQL_ROW row;
std::cout << "您选择了删除学生信息" << std::endl;
//std::cout << "请输入您要删除的学生信息" << std::endl;
std::cout << "\t 1. 按学号删除" << std::endl;
std::cout << "\t 2. 按名字删除" << std::endl;
std::cout << "\t 3. 按照分数删除" << std::endl;
std::cin >> choice;
switch (choice) {
case 1:
{
std::cout << "请输入该生学号" << std::endl;
std::cin >> Id;
sql1 = "select * from Student1 where Id =" + std::to_string(Id);
mysql_query(&mysql, sql1.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row == NULL)
{
std::cout << "不存在该学号的学生" << std::endl;
break;
}
else
sql2 = "delete from Student1 where Id= " + std::to_string(Id);
std::cout << "删除的SQL语句如下:" << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql2.c_str());
break;
}
case 2:
{
std::cout << "请输入该生姓名" << std::endl;
std::cin >> name;
sql1 = "select * from Student1 where name ='" + name + "'";
mysql_query(&mysql, sql1.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row == NULL)
{
std::cout << "不存在该姓名的学生" << std::endl;
break;
}
else
sql2 = "delete from Student1 where name= '" + name + "'";
std::cout << "删除的SQL语句如下:" << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql2.c_str());
break;
}
case 3:
{
std::cout << "您确定要使用分数来删除信息吗,该操作可能会影响多名学生" << std::endl;
std::cout << "1.确定\t 2.取消" << std::endl;
int c = 0;
std::cin >> c;
if (c != 1)
{
std::cout << "您选择了取消操作" << std::endl; break;
}
else
std::cout << "您依旧选择了使用分数删除信息!" << std::endl;
std::cout << "请选择要删除的分数" << std::endl;
std::cin >> score;
sql1 = "select * from Student1 where score =" + std::to_string(score);
mysql_query(&mysql, sql1.c_str());
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
if (row == NULL)
{
std::cout << "不存在该分数的学生" << std::endl;
break;
}
else
sql2 = "delete from Student1 where score= " + std::to_string(score);
std::cout << "删除的SQL语句如下:" << std::endl << sql2 << std::endl; // 检查语句
mysql_query(&mysql, sql2.c_str());
break;
}
default:
std::cout << "您输入的选项有误,请重新输入" << std::endl;
}
}
Students.cpp
cpp
#include "Student.h"
int Student::getId() {
return Id;
}
std::string Student::getName() {
return name;
}
int Student::getScore() {
return score;
}
void Student::setId(int &id) {
Id=id;
}
void Student::setName(std::string& Name) {
name=Name;
}
void Student::setScore(int &Score) {
score=Score;
}
student2.txt
cpp
学号 姓名 成绩
21 张三 98
22 李四 45
CMakeLists.txt
cpp
cmake_minimum_required(VERSION 3.20)
project(new_projects)
set(CMAKE_CXX_STANDARD 17)
include_directories(include)
include_directories(D:/develop/MySQL/MySQL\ Server\ 8.0/include)
link_directories(D:/develop/MySQL/MySQL\ Server\ 8.0/lib)
link_libraries(libmysql)
aux_source_directory(src DIR_SRCS)
add_executable(new_projects ${DIR_SRCS})
AUTO_INCREMENT是主键,主键具有唯一性