-
Notifications
You must be signed in to change notification settings - Fork 0
/
expense.cpp
62 lines (52 loc) · 1.32 KB
/
expense.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
#include "Expense.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
Expense::Expense(const QString &category, const QDate &date, const QString &title, double amount, const QString &description)
{
m_category = category;
m_date = date;
m_title = title;
m_amount = amount;
m_description = description;
}
bool Expense::addToDatabase(QSqlDatabase &db)
{
if (!db.isOpen()) {
qDebug() << "Database is not open";
return false;
}
QSqlQuery query;
query.prepare("INSERT INTO expenses (category, date, title, amount, description) "
"VALUES (:category, :date, :title, :amount, :description)");
query.bindValue(":category", m_category);
query.bindValue(":date", m_date);
query.bindValue(":title", m_title);
query.bindValue(":amount", m_amount);
query.bindValue(":description", m_description);
if (!query.exec()) {
qDebug() << "Error adding expense to database:" << query.lastError();
return false;
}
return true;
}
QString Expense::getCategory() const
{
return m_category;
}
QDate Expense::getDate() const
{
return m_date;
}
QString Expense::getTitle() const
{
return m_title;
}
double Expense::getAmount() const
{
return m_amount;
}
QString Expense::getDescription() const
{
return m_description;
}