-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbmanager.cpp
More file actions
68 lines (59 loc) · 1.64 KB
/
Copy pathdbmanager.cpp
File metadata and controls
68 lines (59 loc) · 1.64 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
#include "dbmanager.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QDebug>
DBManager::DBManager() {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("user.db");
if (!db.open()) {
qDebug() << "데이터베이스 연결 실패:" << db.lastError().text();
} else {
qDebug() << "데이터베이스 연결 성공!";
init();
}
}
DBManager& DBManager::instance() {
static DBManager instance;
return instance;
}
void DBManager::init() {
QSqlQuery query;
QString createTable = R"(
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE,
password TEXT
)
)";
if (!query.exec(createTable)) {
qDebug() << "테이블 생성 실패:" << query.lastError().text();
}
}
bool DBManager::connectToDatabase() {
if (!db.isOpen()) {
return db.open();
}
return true;
}
bool DBManager::addUser(const QString &email, const QString &password) {
QSqlQuery query;
query.prepare("INSERT INTO users (email, password) VALUES (?, ?)");
query.addBindValue(email);
query.addBindValue(password);
if (!query.exec()) {
qDebug() << "회원가입 실패:" << query.lastError().text();
return false;
}
return true;
}
bool DBManager::isValidUser(const QString &email, const QString &password) {
QSqlQuery query;
query.prepare("SELECT * FROM users WHERE email = ? AND password = ?");
query.addBindValue(email);
query.addBindValue(password);
if (query.exec() && query.next()) {
return true;
}
return false;
}