MYSQL+VC++增删改查(三)增添与查询

今天实现了studentDao类的

Select(string str, int flag);  flag = 1表示str为id,flag = 2表示str为name,查询语句不一样

Add(student stu);

然后稍微改了下项目目录方便以后添加新的东西

model放描述数据库表的类,DAO放对表的操作的类。

好的布局还是很有用的。。

附上代码,明天就应该写完了  = .= 

//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) {
		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)\n", query);
		conn::Close(con);
		return 1;
	}
	conn::Close(con);
	return 1;
}

int studentDao::Seeall() {
	MYSQL *con = conn::Connect();
	if (!con) {
		conn::Close(con);
		printf("Seeall conn 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) {
	return 1;
}

int studentDao::Update(string str, int flag) {
	return 1;
}

int studentDao::Select(string str, int flag) {
	MYSQL *con = conn::Connect();
	if (!con) {
		printf("Select %s error !\n", str.c_str());
	}
	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;
			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;
	}
	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;
			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;
	}
	return 0;
}

写了一个丑陋的main.cpp 233

#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");
		printf("id\t name\n");
		dao.Seeall();
		for (int i = 0; i < 10; i++) {
			printf("\n");
		}
		printf("选择要执行的操作\n");
		printf("增加(1)\t删除(2)\t修改(3)\t查询(4)\t退出(0)\n");
		int method;
		cin >> method;
		if (method == 1) {
			while (1) {
				system("cls");
				printf("增加\n\n输入要增加的学生的学号,姓名\n");
				string id, name;
				//输入判断
				cin >> id >> name;
				student stu(id, name);
				//cout << stu.getId() << " " << stu.getName() << endl;
				dao.Add(stu);
				_getch();
				_getch();
				printf("是否退出(输入y退出)?\n");
				char x = _getch();
				if (x == 'y')
					break;
			}
		}
		else if (method == 4) {
			while (1) {
				system("cls");
				string name, id;
				printf("查询\n\n根据id查询(1) 根据姓名查询(2)\n");
				cin >> method;
				if (method == 1) {
					printf("请输入id:\n");
					cin >> id;
					dao.Select(id, method);
				}
				else if (method == 2){
					printf("请输入姓名:\n");
					cin >> name;
					dao.Select(name, method);
				}
				else {
					printf("错误输入 !!!\n");
				}
				_getch();
				_getch();
				printf("是否退出(输入y退出)?");
				char x = _getch();
				if (x == 'y')
					break;
			}
		}
		system("cls");
	}
	system("pause");
	return 0;
}

注意下_getch();的使用。。。运行出来大概是这个样子。。。

然后的话,其实还有很多需要注意的点我没写出来,用户输入的时候可能会输入错误的值,这是要处理的地方,比如id应该是数字,执行的操作输入的应该也是数字,在表里添加数据的时候是不是要先查询这个表里有没有这条数据,错误处理也是很重要的一环。

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值