-
Notifications
You must be signed in to change notification settings - Fork 1
/
databasemanager.cpp
160 lines (131 loc) · 4.63 KB
/
databasemanager.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "databasemanager.h"
#include "qsqlerror.h"
#include<QMap>
DatabaseManager::DatabaseManager(QObject *parent):QObject(parent)
{
initializeDatabase();
}
bool DatabaseManager::initializeDatabase()
{
/*
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//存到userData文件夹
db.setDatabaseName(name+"syncTasks.db");
if (!db.open()) {
qDebug() << "Database open failed:" << db.lastError();
return;
}
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS SyncTasks (id INTEGER PRIMARY KEY AUTOINCREMENT, localPath TEXT, remotePath TEXT, syncStatus INTEGER)")) {
qDebug() << "Failed to create table:" << query.lastError();
}*/
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("Database.db");
if(!db.open())
{
qWarning() << "Database open failed:" << db.lastError();
return false;
}
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS tmd (account TEXT PRIMARY KEY, hashedPassword TEXT);")) {
qWarning() << "Failed to create table:" << query.lastError();
return false;
}
query.prepare("SELECT account, hashedPassword FROM tmd");
if(query.exec())
{
while (query.next()) {
accountPasswordMap_[query.value(0).toString()] = query.value(1).toString();
}
}
else
{
qWarning() << "Failed to get all accounts:" << query.lastError();
}
return true;
}
bool DatabaseManager::insertUser(const QString &account, const QString &hashedPassword)
{
QSqlQuery query;
/*query.prepare("INSERT INTO tmd (account, hashedPassword) VALUES (:account, :hashedPassword)");
query.bindValue(":account", account);
query.bindValue(":hashedPassword", hashedPassword);*/
QString strs=QString("INSERT INTO tmd VALUES('%1','%2')").arg(account).arg(hashedPassword);
if (!query.exec(strs)) {
qWarning() << "Failed to insert user:" << query.lastError();
return false;
}
// 成功插入新用户后,更新账号密码映射
accountPasswordMap_[account] = hashedPassword;
return true;
}
bool DatabaseManager::updateUserInfo(const QString &account,const QString &newHashedPassword)
{
QSqlQuery query;
bool transactionSuccess = false;
// 确保数据库连接是打开的
if (!QSqlDatabase::database().isOpen()) {
qWarning() << "Database is not open.";
return false;
}
if(!query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='tmd'")){
qDebug()<<"table not exist";
}
// 开始一个事务
if (QSqlDatabase::database().transaction()) {
// 准备 SQL 语句
if (!query.prepare("UPDATE tmd SET hashedPassword = ? WHERE account = ?")) {
qDebug() << "Prepare failed:" << query.lastError();
}
query.addBindValue(newHashedPassword);
query.addBindValue(account);
if (!query.exec()) {
qDebug() << "Execution failed:" << query.lastError();
}
// 执行查询
if (!query.exec()) {
// 如果执行失败,打印错误信息
qWarning() << "Failed to update user info:" << query.lastError().text();
// 回滚事务
QSqlDatabase::database().rollback();
return false;
} else {
// 数据库更新成功,更新映射
accountPasswordMap_[account] = newHashedPassword;
// 提交事务
transactionSuccess = QSqlDatabase::database().commit();
if (!transactionSuccess) {
qWarning() << "Failed to commit transaction:" << QSqlDatabase::database().lastError().text();
}
}
} else {
qWarning() << "Failed to start transaction:" << QSqlDatabase::database().lastError().text();
return false;
}
return transactionSuccess;
}
QList<QString> DatabaseManager::getAllAccounts()
{
QSqlQuery query;
QList<QString> accounts;
query.prepare("SELECT account FROM tmd");
if (query.exec()) {
while (query.next()) {
accounts.append(query.value(0).toString());
}
} else {
qWarning() << "Failed to get all accounts:" << query.lastError();
}
return accounts;
}
QPair<QString, QString> DatabaseManager::getUserPassword(const QString &account)
{
QPair<QString, QString> userPassword;
userPassword.first = account; // 账号
if (accountPasswordMap_.contains(account)) {
userPassword.second = accountPasswordMap_[account]; // 密码
return userPassword;
}
// 如果没有找到匹配的账号,返回空值
return userPassword;
}