-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
109 lines (96 loc) · 3.08 KB
/
database.cpp
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
#include "database.h"
DataBase::DataBase()
{
initDataBase();
}
void DataBase::initDataBase()
{
// 打印Qt支持的数据库驱动
qDebug() << QSqlDatabase::drivers();
// 添加数据库
db = QSqlDatabase::addDatabase("QSQLITE");
// 设置数据库
QString db_path = "C:/Users/92502/Desktop/Lab4a.db";
db.setDatabaseName(db_path);
// 打开数据库
if (!db.open()){
qDebug() << "不能连接 connect to mysql error" << db.lastError().text();
return;
}else {
qDebug() << "连接成功 connect to mysql OK";
}
}
bool DataBase::initPatientModel()
{
patientTabModel = std::make_unique<QSqlTableModel>(this,db);
patientTabModel->setTable("Patient");
patientTabModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
patientTabModel->setSort(patientTabModel->fieldIndex("NAME"), Qt::AscendingOrder);
if (!patientTabModel->select()){
qDebug()<<"Failed to select data from patient table" << patientTabModel->lastError().text();
return false;
}
patientSelection = std::make_unique<QItemSelectionModel>(patientTabModel.get());
return true;
}
QString DataBase::userLogin(QString username, QString password)
{
QSqlQuery query;
qDebug()<<username <<" " << password;
QString qs = QString("select * from User where USERNAME = '%1' and PASSWORD = '%2'").arg(username).arg(password);
query.exec(qs);
if (query.next()){
return "LoginSuccess";
}else {
return "LoginFail";
}
}
bool DataBase::searchPatient(const QString &filter)
{
patientTabModel->setFilter(filter);
return patientTabModel->select();
}
bool DataBase::deleteCurrentPatient()
{
QModelIndex curIndex = patientSelection->currentIndex();
if (curIndex.isValid()){
patientTabModel->removeRow(curIndex.row());
if (patientTabModel->submitAll()){
patientTabModel->select();
return true;
}else {
qDebug()<<"Failed to submit changes to the patient table";
return false;
}
}else {
qDebug()<<"Invaild current index";
return false;
}
}
int DataBase::addNewPatient()
{
int rowCount = patientTabModel->rowCount();
patientTabModel->insertRow(rowCount, QModelIndex()); // 在末尾添加一行
QModelIndex curIndex = patientTabModel->index(rowCount, 1); // 创建最后一行的modelIndex
int curRecNo = curIndex.row();
QSqlRecord curRec = patientTabModel->record(curRecNo); // 获取当前记录
curRec.setValue("CREATEDTIMESTAMP", QDateTime::currentDateTime().toString("yyyy-MM-dd"));
curRec.setValue("ID", QUuid::createUuid().toString(QUuid::WithBraces));
patientTabModel->setRecord(curRecNo, curRec);
return curIndex.row();
}
bool DataBase::submitPatientEdit()
{
if (patientTabModel->submitAll()){
qDebug() << "Patient data submitted successfully";
return true;
}else {
qDebug() << "Failed to submit patient data";
return false;
}
}
void DataBase::revertPatientEdit()
{
patientTabModel->revertAll();
qDebug() << "Patient data reverted";
}