-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabaseManager.h
More file actions
52 lines (42 loc) · 1.28 KB
/
DatabaseManager.h
File metadata and controls
52 lines (42 loc) · 1.28 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
#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QDebug>
class DatabaseManager {
public:
// Public method to get the singleton instance
static DatabaseManager& instance() {
static DatabaseManager instance;
return instance;
}
// Method to get the database connection
QSqlDatabase getDatabase() const {
return db;
}
// Method to close the database connection
void closeConnection() {
if (db.isOpen()) {
db.close();
}
}
private:
QSqlDatabase db;
// Private constructor for singleton pattern
DatabaseManager() {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)+"/i_m_s.db");
if (!db.open()) {
qDebug() << "Error opening database: " << db.lastError().text();
} else {
qDebug() << "Database connected successfully!";
}
}
// Delete copy constructor and assignment operator
DatabaseManager(const DatabaseManager&) = delete;
DatabaseManager& operator=(const DatabaseManager&) = delete;
};
#endif // DATABASEMANAGER_H