先创建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;
}