MYSQL+VC++增删改查(二)简单查询

先创建student表对应的类

 

//student.h
#pragma once
#include <string>
using namespace std;
class student
{
private:
	string id;
	string name;
	string password;
public:
	student();
	student(string id, string name, string password);
	~student();
	string getName();
	string getId();
	string getPassword();

	void setName(string _name);
	void setId(string _id);
	void setPassword(string _password);
};

类的方法实现

//student.cpp
#include "student.h"
#include <iostream>

using namespace std;

student::student() {

}

student::~student() {

}

student::student(string _id, string _name, string _password) {
	this->id = _id;
	this->name = _name;
	this->password = _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;
}

string student::getPassword() {
	if (this->password == "") {
		cout << "getPassword error !" << endl;
	}
	return this->password;
}
 
void student::setPassword(string _password) {
	if (_password == "") {
		cout << "setPassword error !" << endl;
	}
	this->password = _password;
}

然后创建studentDao类,用于实现对学生表的查询

//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);//flag=1表示按照id查询,等于2表示按照name
	int Update(string str, int flag);
	int Select(string str, int flag);
	int Seeall();
};

今天只实现了最简单的Seeall()函数,就是查看表里所有的内容。

//studentDao.cpp
#include "studentDao.h"
#include <cstdio>
#include <iostream>
using namespace std;

studentDao::studentDao() {

}


studentDao::~studentDao() {

}

int studentDao::Add(student stu) {
	return 0;
}

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 0;
}
int studentDao::Delete(string str, int flag) {
	return 0;
}

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

int studentDao::Select(string str, int flag) {
	return 0;
}

然后在主函数写下如下,就可以查看到表里的内容了

//main.cpp
#include <cstdio>
#include "student.h"
#include "studentDao.h"

using namespace std;
int main() {
	studentDao dao;
	dao.Seeall();
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值