Skip to content

Commit

Permalink
Signed-off-by: whelanh
Browse files Browse the repository at this point in the history
  • Loading branch information
whelanh committed Aug 4, 2016
1 parent 28c1f09 commit 7512492
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
43 changes: 20 additions & 23 deletions dbmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
#include <QDebug>
#include <QDate>



DbManager::DbManager(const QString &path)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(path);

if (!m_db.open())
{
qDebug() << "Error: connection with database fail";
// qDebug() << "Error: connection with database failed";
} else
{
qDebug() << "Database: connection ok";
// qDebug() << "Database: connection ok";
}
}

Expand All @@ -37,7 +35,7 @@ bool DbManager::isOpen() const

QString DbManager::printAllRecords() const
{
qDebug() << "all records db:";
// qDebug() << "all records db:";
QSqlQuery query("SELECT Date, DayOfWeek, Entry FROM journal Order by Date DESC");
QString answer;
while (query.next())
Expand All @@ -58,54 +56,54 @@ QString DbManager::printAllRecords() const

void DbManager::getLastRecord()
{
qDebug() << "Last entry in journal:";
// qDebug() << "Last entry in journal:";
QSqlQuery query("SELECT ID, Date, Entry FROM journal ORDER BY Date DESC LIMIT 1;");
int idName = query.record().indexOf("Entry");
while (query.next())
{
QString name = query.value(idName).toString();
qDebug() << "===" << name;
// qDebug() << "===" << name;
lastEntry = name;
idName = query.record().indexOf("Date");
QString lastdate = query.value(idName).toString();
lastDate = lastdate;
idName = query.record().indexOf("ID");
int lastid = query.value(idName).toInt();
lastID = lastid;
qDebug() << "last ID:" << lastID;
// qDebug() << "last ID:" << lastID;
}
}

void DbManager::getRecordOnDate(const QDate &date)
{
bool success = false;

qDebug() << "Entry on Date:";
// qDebug() << "Entry on Date:";
QString dt = date.toString("yyyy-MM-dd");
qDebug() << dt;
// qDebug() << dt;
QSqlQuery query;
query.prepare("SELECT ID, Date, Entry FROM journal WHERE Date = (:dt)");
query.bindValue(":dt", dt);
success = query.exec();

if(!success) {
qDebug() << "query failed";
// qDebug() << "query failed";
} else {
lastEntry = "";
lastID = 0;
lastDate = dt;
while (query.next()) {
int idName = query.record().indexOf("Entry");
QString name = query.value(idName).toString();
qDebug() << "===" << name;
// qDebug() << "===" << name;
lastEntry = name;
idName = query.record().indexOf("Date");
QString lastdate = query.value(idName).toString();
lastDate = lastdate;
idName = query.record().indexOf("ID");
int lastid = query.value(idName).toInt();
lastID = lastid;
qDebug() << "last ID:" << lastID;
// qDebug() << "last ID:" << lastID;
}
}
}
Expand All @@ -119,7 +117,7 @@ QString DbManager::searchTermQuery(const QString &term)
QString q("SELECT Date, DayOfWeek, Entry from journal where (journal.Entry LIKE ?) order by Date");
query.prepare(q);
query.addBindValue(QString("\%" + term + "\%"));
qDebug() << q << " term:" << term;
// qDebug() << q << " term:" << term;
success = query.exec();
QString answer;

Expand Down Expand Up @@ -221,9 +219,9 @@ void DbManager::writeRecord(int &id, QString &dt, QString &mnth, int &dy, int &y
success = qupdate.exec();

if (success) {
qDebug() << "existing record updated";
// qDebug() << "existing record updated";
} else {
qDebug() << "existing record update failed" << qupdate.lastError();
// qDebug() << "existing record update failed" << qupdate.lastError();
}
}
else
Expand All @@ -240,17 +238,16 @@ void DbManager::writeRecord(int &id, QString &dt, QString &mnth, int &dy, int &y
query.bindValue(":ein", entry);
success = query.exec();
if (success) {
qDebug() << "added new record";
// qDebug() << "added new record";
} else {
qDebug() << "new record add failed" << query.lastError();
// qDebug() << "new record add failed" << query.lastError();
}
}
} else {
qDebug() << "check query failed" << checkQuery.lastError();
// qDebug() << "check query failed" << checkQuery.lastError();
}
}


QMap<QString, QString> DbManager::getWeightRecord()
{
bool success = false;
Expand All @@ -273,7 +270,7 @@ QMap<QString, QString> DbManager::getWeightRecord()
wtMap[date] = match.captured(0);
break;
}
// qDebug() << "Date:" << date << ": " << word;
// // qDebug() << "Date:" << date << ": " << word;
}
}
}
Expand All @@ -284,14 +281,14 @@ QMap<QString, QString> DbManager::getWeightRecord()
QString DbManager::getLastRecordDate() const
{
QString name;
qDebug() << "last date in journal";
// qDebug() << "last date in journal";
QSqlQuery query("SELECT Date, Entry FROM journal ORDER BY Date DESC LIMIT 1;");
int idName = query.record().indexOf("Date");
while (query.next())
{
name = query.value(idName).toString();

qDebug() << "===" << name;
// qDebug() << "===" << name;
}
return name;

Expand Down
3 changes: 1 addition & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <QMessageBox>
#include <QSettings>


static QString PATH = "";

MainWindow::MainWindow(QWidget *parent) :
Expand Down Expand Up @@ -95,7 +94,7 @@ void MainWindow::on_weightHistoryButton_clicked()
QMapIterator<QString, QString> i(map);
QString answer;
QString filename = QFileDialog::getSaveFileName(this,
"Save To CSV", "weightHistory.csv", "CSV files (.csv);;Zip files (.zip, *.7z)", 0, 0); // getting the filename (full path)
"Save To CSV", "weightHistory.csv", "CSV files (.csv);;Zip files (.zip, *.7z)", 0, 0); // getting the filename (full path)
QFile data(filename);
if(data.open(QFile::WriteOnly |QFile::Truncate)) {
QTextStream output(&data);
Expand Down

0 comments on commit 7512492

Please sign in to comment.