-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
94 lines (77 loc) · 2.92 KB
/
Copy pathdatabase.cpp
File metadata and controls
94 lines (77 loc) · 2.92 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
86
87
88
89
90
91
92
#include "database.h"
Database::Database() {
// Constructor calls the openDatabase function to initialize the DB connection
if (!openDatabase()) {
qDebug() << "Database could not be opened!";
} else {
qDebug() << "Database opened successfully.";
}
}
bool Database::openDatabase() {
// Set the database path in the writable AppData location
QString dbPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/user_credentials.db";
QDir().mkpath(QFileInfo(dbPath).path()); // Ensure directory exists
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbPath);
if (!db.open()) {
qDebug() << "Error: Unable to open the database!" << db.lastError().text();
return false;
}
return true;
}
bool Database::createUserTable() {
QSqlQuery query;
QString createTableQuery = R"(
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
)
)";
if (!query.exec(createTableQuery)) {
qDebug() << "Error creating table:" << query.lastError().text();
return false;
}
qDebug() << "User table created successfully.";
return true;
}
bool Database::insertUser(const QString &username, const QString &email, const QString &password)
{
QSqlQuery query;
// Prepare the insert query with placeholders for username, email, and password
query.prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
// Bind the actual values to the placeholders
query.addBindValue(username); // Bind username value
query.addBindValue(email); // Bind email value (this is the new parameter added)
query.addBindValue(password); // Bind password value
// Execute the query and check for errors
if (!query.exec()) {
qDebug() << "Error inserting user: " << query.lastError();
} else {
qDebug() << "User inserted successfully!";
}
return true;
}
void Database::fetchUsers() {
QSqlQuery query("SELECT id, username FROM users");
while (query.next()) {
int id = query.value(0).toInt();
QString username = query.value(1).toString();
qDebug() << "User ID:" << id << ", Username:" << username;
}
}
QSqlQuery Database::getUserByIdentifier(const QString &identifier) {
QSqlQuery query;
// Check if identifier contains '@' to distinguish email from username
if (identifier.contains('@')) {
query.prepare("SELECT username, email, password FROM users WHERE email = :identifier");
} else {
query.prepare("SELECT username, email, password FROM users WHERE username = :identifier");
}
query.bindValue(":identifier", identifier);
if (!query.exec()) {
qDebug() << "Error executing query:" << query.lastError().text();
}
return query;
}