-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbman.cpp
More file actions
86 lines (71 loc) · 2.87 KB
/
dbman.cpp
File metadata and controls
86 lines (71 loc) · 2.87 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
#include "dbman.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QDateTime>
#include "patientsqlmodel.h"
#include <QQmlContext>
void DbMan::setGlobalViewer(QQuickView *view)
{
viewer = view ;
}
PatientSqlModel *DbMan::model(QString customerName, QString customerPhone)
{
if (!createConnection()){
qDebug()<<"Failed";
}
patientModel = new PatientSqlModel(this, customerName, customerPhone);
return patientModel;
}
void DbMan::reloadModel()
{
viewer->rootContext ()->setContextProperty("patientmodel", this->model ());
}
void DbMan::reloadModel(QString customerName,QString customerPhone)
{
viewer->rootContext ()->setContextProperty("patientmodel", this->model (customerName,customerPhone));
}
DbMan::DbMan(QObject *parent) :
QObject(parent)
{
}
void DbMan::addInfo(QString devCode, QString patientName, QString patientPhone, qreal leftEyeSPH, qreal leftEyeCyl, qreal leftEyeAx, qreal rightEyeSPH, qreal rightEyeCyl, qreal rightEyeAx, QString lensType, QString detail, bool syncState)
{
if (!createConnection())
return;
QSqlQuery query;
query.prepare("INSERT INTO patientinfo (date, deviceCode , name , phone , leftEyeSph ,leftEyeCyl ,leftEyeAx , rightEyeSph ,rightEyeCyl , rightEyeAx, lensType, detail, synced)"
" VALUES (:CurrentDateTime, :DeviceCode, :PatientName,:PatientPhone ,:LeftEyeSPH, :LeftEyeCyl, :LeftEyeAx,:RightEyeSPH, :RightEyeCyl, :RightEyeAx, :LensType, :Detail, :SyncState)");
query.bindValue(":CurrentDateTime", QDateTime::currentDateTime());
query.bindValue(":DeviceCode", devCode);
query.bindValue(":PatientName", patientName );
query.bindValue(":PatientPhone", patientPhone);
query.bindValue(":LeftEyeSPH", leftEyeSPH);
query.bindValue(":LeftEyeCyl", leftEyeCyl);
query.bindValue(":LeftEyeAx", leftEyeAx);
query.bindValue(":RightEyeSPH", rightEyeSPH);
query.bindValue(":RightEyeCyl", rightEyeCyl);
query.bindValue(":RightEyeAx", rightEyeAx);
query.bindValue(":LensType", lensType);
query.bindValue(":Detail", detail);
query.bindValue(":SyncState", syncState); //cloud backend
query.exec();
}
bool DbMan::createConnection()
{
if ( QSqlDatabase::database().isOpen ()) {
return true;
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //default connection
db.setDatabaseName("data/database.db");
if (!db.open()){
qDebug()<<db.lastError();
return false;
}
QSqlQuery query;
query.exec("create table patientinfo (date datetime,deviceCode varchar(4), name varchar(25), phone varchar(15) , leftEyeSph NUMERIC(2,2),leftEyeCyl NUMERIC(2,2),leftEyeAx NUMERIC(2,2), rightEyeSph NUMERIC(2,2),rightEyeCyl NUMERIC(2,2),rightEyeAx NUMERIC(2,2),"
" lensType varchar(6),detail varchar(200), synced bool DEFAULT ('false'))");
return true;
}