-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentDao.cpp
More file actions
147 lines (138 loc) · 4.99 KB
/
StudentDao.cpp
File metadata and controls
147 lines (138 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "StudentDao.h"
StudentDAO::StudentDAO(){}
bool StudentDAO::insert(Student student)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return false;
//construct the database query object
QSqlQuery query;
query.prepare("insert into studentInfo(studentID,name,major,UID) values(?,?,?,?)");
query.bindValue(0,Util::toQString(student.getId()));
query.bindValue(1,Util::toQString(student.getName()));
query.bindValue(2,Util::toQString(student.getMarjor()));
query.bindValue(3,Util::toQString(student.getUId()));
//get the result of execute the query string
bool succ=query.exec();
if(succ) qDebug()<<"插入成功";
else qDebug()<<"插入失败"<<query.lastError();
return succ;
}
list<Student> StudentDAO::findAll()
{
list<Student> students;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("select * from studentInfo");
//get the result of execute the query string
bool succ=query.exec();
int numRows=succ?0:-1;
if(dbSQL.driver()->hasFeature(QSqlDriver::QuerySize)){
numRows=query.size();
} else{
query.last();
numRows=query.at()+1;
}
Student stu;
while(query.next()){
stu.setId(query.value(0).toString().toStdString());
stu.setName(query.value(1).toString().toStdString());
stu.setMarjor(query.value(2).toString().toStdString());
stu.setUId(query.value(3).toString().toStdString());
students.push_back(stu);
}
DatabaseConnection::close(dbSQL);
return students;
}
Student StudentDAO::findById(string id)
{
Student stu;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return stu;
//construct the database query object
QSqlQuery query;
query.prepare("select * from studentInfo where studentID=?");
query.bindValue(0,QString::fromStdString(id));
//get the result of execute the query string
bool succ=query.exec();
if(!succ) return stu;
if(query.next()){
stu.setId(query.value(0).toString().toStdString());
stu.setName(query.value(1).toString().toStdString());
stu.setMarjor(query.value(2).toString().toStdString());
stu.setUId(query.value(3).toString().toStdString());
}
DatabaseConnection::close(dbSQL);
return stu;
}
bool StudentDAO::deleteById(string id)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("delete from studentInfo where studentID=?");
query.bindValue(0,Util::toQString(id));
//get the result of execute the query string
bool succ=query.exec();
DatabaseConnection::close(dbSQL);
return succ;
}
bool StudentDAO::update(Student student)
{
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
//construct the database query object
QSqlQuery query;
query.prepare("update studentInfo set name=?,major=?,UID=? where studentID=?");
query.bindValue(3,Util::toQString(student.getId()));
query.bindValue(0,Util::toQString(student.getName()));
query.bindValue(1,Util::toQString(student.getMarjor()));
query.bindValue(2,Util::toQString(student.getUId()));
//get the result of execute the query string
bool succ=query.exec();
DatabaseConnection::close(dbSQL);
return succ;
}
bool StudentDAO::queryById(string id)
{
bool flag=false;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return flag;
//construct the database query object
QSqlQuery query;
query.prepare("select * from studentInfo where studentID=?");
query.bindValue(0,QString::fromStdString(id));
//get the result of execute the query string
bool succ=query.exec();
if(!succ) return flag;
if(query.next())flag=true;
DatabaseConnection::close(dbSQL);
return flag;
}
Student StudentDAO::findByUId(string uid)
{
Student stu;
//get database connection
QSqlDatabase dbSQL=DatabaseConnection::getConnection();
if(!dbSQL.isOpen()) return stu;
//construct the database query object
QSqlQuery query;
query.prepare("select * from studentInfo where UID=?");
query.bindValue(0,QString::fromStdString(uid));
//get the result of execute the query string
bool succ=query.exec();
if(!succ) return stu;
if(query.next()){
stu.setId(query.value(0).toString().toStdString());
stu.setName(query.value(1).toString().toStdString());
stu.setMarjor(query.value(2).toString().toStdString());
stu.setUId(query.value(3).toString().toStdString());
}
DatabaseConnection::close(dbSQL);
return stu;
}