今天写了删和改,然后修改了前面的一些错误以及写的不好的地方,当然了这只是个很low的练手小项目,虽然有考虑过用QT写,但是实在不想弄界面了,= = 凑合下总是可以的。重要的是希望能帮到一些做刚学数据库的朋友吧(虽然我也是菜鸡)。。
没有特别仔细的去测试功能,样例就随便试了下,可能会存在bug。。
数据库很简单只有一个表,不过只要会了最基础的操作,其它的大部分都是逻辑上的问题了。
name :test
tables :student 属性 name id 类型都是varchar
//studentDao.h
#pragma once
#include "student.h"
#include "conn.h"
class studentDao
{
public:
studentDao();
~studentDao();
int Add(student stu);
int Delete(string str, int flag);
int Update(student stu);
int Select(string str, int flag);
int Seeall();
};
//student.h
#pragma once
#include <string>
using namespace std;
class student
{
private:
string id;
string name;
public:
student();
student(string id, string name);
~student();
string getName();
string getId();
void setName(string _name);
void setId(string _id);
};
//conn.h
#pragma once
#include <mysql.h>
class conn
{
public:
conn();
~conn();
static MYSQL* Connect();
static void Close(MYSQL *con);
};
//conn.cpp
#include <mysql.h>
#include <iostream>
#include "conn.h"
MYSQL* conn::Connect() {
MYSQL* con = mysql_init((MYSQL*)0);
char dbuser[30] = "root";
char dbpasswd[30] = "123456"; // it must be changed
char dbip[30] = "localhost";
char dbname[50] = "test";
int count = 0;
con = mysql_init((MYSQL*)0);
if (con != NULL && mysql_real_connect(con, dbip, dbuser, dbpasswd, dbname, 3306, NULL, 0)) {
//printf("connect success!\n");
return con;
}
else {
printf("connect failed!\n");
return nullptr;
}
}
void conn::Close(MYSQL *con) {
mysql_close(con);
}
conn::conn() {
}
conn::~conn() {
}
//student.cpp
#include "student.h"
#include <iostream>
using namespace std;
student::student() {
}
student::~student() {
}
student::student(string _id, string _name) {
this->id = _id;
this->name = _name;
}
string student::getName() {
if (this->name == "") {
cout << "getName error !" << endl;
}
return this->name;
}
void student::setName(string _name) {
if (_name == "") {
cout << "setName error" << endl;
return;
}
this->name = _name;
}
string student::getId() {
if (this->id == "") {
cout << "getId error !" << endl;
}
return this->id;
}
void student::setId(string _id) {
if (_id == "") {
cout << "setId error" << endl;
return;
}
this->id = _id;
}
//studentDao.cpp
#include "studentDao.h"
#include <cstdio>
#include <iostream>
using namespace std;
studentDao::studentDao() {
}
studentDao::~studentDao() {
}
int studentDao::Add(student stu) {
MYSQL *con = conn::Connect();
if (con == nullptr) {
printf("Add connect error");
conn::Close(con);
return 0;
}
string tmp = "insert into student(id, name) values ('";
tmp += stu.getId();
tmp += "','";
tmp += stu.getName();
tmp += "');";
char* query = new char[tmp.size() + 1];
std::strcpy(query, tmp.c_str());
if (mysql_query(con, query)) {
printf("Add failed (%s)\n", query);
conn::Close(con);
return 0;
}
else {
printf("Add success %s %s\n", stu.getId().c_str(), stu.getName().c_str());
conn::Close(con);
return 1;
}
conn::Close(con);
return 1;
}
int studentDao::Seeall() {
MYSQL *con = conn::Connect();
if (!con) {
conn::Close(con);
printf("Seeall connect error\n");
return 0;
}
if (mysql_query(con, "select * from student;")) {
printf("Seeall query error\n");
}
else {
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row;
int flag = 0;
while (row = mysql_fetch_row(result)) {
flag = 1;
for (int i = 0; i < 2; i++)
printf("%s\t", row[i]);
printf("\n");
}
if (flag == 0) {
cout << "no result !" << endl;
}
}
conn::Close(con);
return 1;
}
int studentDao::Delete(string str, int flag) {
if(!this->Select(str, flag)) {
return 0;
}
MYSQL *con = conn::Connect();
if(!con) {
printf("Delete connect error !\n");
conn::Close(con);
}
string query = "delete from student where ";
if (flag == 1) {
query += "id = '";
query += str;
query += "';";
}
else {
query += "name = '";
query += str;
query += "';";
}
if (mysql_query(con, query.c_str())) {
printf("Delete error %s", query.c_str());
conn::Close(con);
return 0;
}
printf("Delete %s seccess\n", str.c_str());
return 0;
}
int studentDao::Update(student stu) {
if (!this->Select(stu.getId(), 1)) {
return 0;
}
MYSQL *con = conn::Connect();
if (!con) {
printf("Update connect error !\n");
conn::Close(con);
return 0;
}
string query = "update student set name = '";
query += stu.getName();
query += "' where id = '";
query += stu.getId();
query += "';";
if (mysql_query(con, query.c_str())) {
printf("Update %s error\n",query.c_str());
conn::Close(con);
return 0;
}
printf("Update seccess %s %s\n",stu.getId().c_str(), stu.getName().c_str());
return 1;
}
int studentDao::Select(string str, int flag) {
MYSQL *con = conn::Connect();
if (!con) {
printf("Select connect error !\n");
}
if (flag == 1) {
string query = "select * from student where id = '";
query += str;
query += "';";
if (mysql_query(con, query.c_str())) {
printf("Select query error\n");
}
else {
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row;
flag = 0;
while (row = mysql_fetch_row(result)) {
flag = 1;
for (int i = 0; i < 2; i++)
printf("%s\t", row[i]);
printf("\n");
}
if (flag == 0) {
cout << "no result !" << endl;
conn::Close(con);
return 0;
}
}
conn::Close(con);
return 1;
}
if (flag == 2) {
string query = "select * from student where name = '";
query += str;
query += "';";
//cout << query << endl;
if (mysql_query(con, query.c_str())) {
printf("Select query error\n");
}
else {
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row;
flag = 0;
while (row = mysql_fetch_row(result)) {
flag = 1;
for (int i = 0; i < 2; i++)
printf("%s\t", row[i]);
printf("\n");
}
if (flag == 0) {
cout << "no result !" << endl;
conn::Close(con);
return 0;
}
}
conn::Close(con);
return 1;
}
return 0;
}
//main.cpp
#include <iostream>
#include <string>
#include <cstdio>
#include <conio.h>
#include "student.h"
#include "studentDao.h"
using namespace std;
int main() {
studentDao dao;
while (1) {
printf("学生管理:\n");
cout << endl;
printf("id\t name");
cout << endl;
dao.Seeall();
for (int i = 0; i < 10; i++) {
cout << endl;
}
printf("选择要执行的操作\n");
printf("增加(1)\t删除(2)\t修改(3)\t查询(4)\t退出(0)");
cout << endl;
int method;
cin >> method;
if (method == 1) {
while (1) {
system("cls");
printf("增加\n\n输入要增加的学生的学号:");
cout << endl;
string id, name;
//输入判断
cin >> id;
printf("输入要增加的学生的姓名:");
cout << endl;
cin >> name;
student stu(id, name);
//cout << stu.getId() << " " << stu.getName() << endl;
dao.Add(stu);
printf("是否退出(输入y退出)?");
cout << endl;
string x;
cin >> x;
if (x == "y")
break;
}
}
else if (method == 2) {
while (1) {
system("cls");
string name, id;
printf("删除\n\n根据id删除(1) 根据姓名删除(2)");
cout << endl;
cin >> method;
if (method == 1) {
printf("请输入id:");
cout << endl;
cin >> id;
dao.Delete(id, method);
}
else if (method == 2) {
printf("请输入姓名:");
cout << endl;
cin >> name;
dao.Delete(name, method);
}
else {
printf("错误输入 !!!");
cout << endl;
}
printf("是否退出(输入y退出)?");
cout << endl;
string x;
cin >> x;
if (x == "y")
break;
}
}
else if (method == 3) {
while (1) {
system("cls");
printf("增加\n\n输入要修改的学生的学号:");
cout << endl;
string id, name;
//输入判断
cin >> id;
printf("输入修改之后的学生的姓名:");
cout << endl;
cin >> name;
student stu(id, name);
//cout << stu.getId() << " " << stu.getName() << endl;
dao.Update(stu);
printf("是否退出(输入y退出)?");
cout << endl;
string x;
cin >> x;
if (x == "y")
break;
}
}
else if (method == 4) {
while (1) {
system("cls");
string name, id;
printf("查询\n\n根据id查询(1) 根据姓名查询(2)");
cout << endl;
cin >> method;
if (method == 1) {
printf("请输入id:");
cout << endl;
cin >> id;
dao.Select(id, method);
}
else if (method == 2){
printf("请输入姓名:");
cout << endl;
cin >> name;
dao.Select(name, method);
}
else {
printf("错误输入 !!!");
cout << endl;
}
printf("是否退出(输入y退出)?");
cout << endl;
string x;
cin >> x;
if (x == "y")
break;
}
}
else if (method == 0) {
break;
}
else {
printf("输入错误\n");
}
system("cls");
}
system("pause");
return 0;
}
可能程序有的地方会抛出异常,遇到这种情况,百度下,很容易解决的,我也忘了是哪里会抛出异常。