From 2601f5714510891168e0f24218557e7e6193b508 Mon Sep 17 00:00:00 2001 From: Cppthx <2728347417@qq.com> Date: Tue, 9 Jul 2024 18:27:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=AE=B0=E4=BD=8F=E5=AF=86=E7=A0=81=E7=9A=84bug,=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E8=83=BD=E5=9C=A8=E6=B3=A8=E5=86=8C=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=B0=B1=E8=AE=B0=E4=BD=8F=E5=AF=86=E7=A0=81?= =?UTF-8?q?,=E9=9C=80=E8=A6=81=E5=9C=A8=E7=99=BB=E9=99=86=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E6=89=8D=E8=83=BD=E8=AE=B0=E4=BD=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- databasemanager.cpp | 31 ++++++------------------------- databasemanager.h | 5 ++--- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/databasemanager.cpp b/databasemanager.cpp index f4d202b..eb6e1cc 100644 --- a/databasemanager.cpp +++ b/databasemanager.cpp @@ -24,7 +24,7 @@ bool DatabaseManager::initializeDatabase() QSqlQuery query; - query.prepare("SELECT account, hashedPassword, avatar FROM Users"); + query.prepare("SELECT account, hashedPassword FROM Users"); if(query.exec()) { while (query.next()) { @@ -36,7 +36,7 @@ bool DatabaseManager::initializeDatabase() qWarning() << "Failed to get all accounts:" << query.lastError(); } - if (!query.exec("CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT UNIQUE, hashedPassword TEXT, avatar TEXT)")) { + if (!query.exec("CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT UNIQUE, hashedPassword TEXT)")) { qWarning() << "Failed to create table:" << query.lastError(); return false; } @@ -44,13 +44,12 @@ bool DatabaseManager::initializeDatabase() return true; } -bool DatabaseManager::insertUser(const QString &account, const QString &hashedPassword, const QString &avatar) +bool DatabaseManager::insertUser(const QString &account, const QString &hashedPassword) { QSqlQuery query; - query.prepare("INSERT INTO Users (account, hashedPassword, avatar) VALUES (:account, :hashedPassword, :avatar)"); + query.prepare("INSERT INTO Users (account, hashedPassword) VALUES (:account, :hashedPassword )"); query.bindValue(":account", account); query.bindValue(":hashedPassword", hashedPassword); - query.bindValue(":avatar", avatar); if (!query.exec()) { qWarning() << "Failed to insert user:" << query.lastError(); @@ -60,12 +59,11 @@ bool DatabaseManager::insertUser(const QString &account, const QString &hashedPa return true; } -bool DatabaseManager::updateUserInfo(const QString &account,const QString &newHashedPassword, const QString &newAvatar) +bool DatabaseManager::updateUserInfo(const QString &account,const QString &newHashedPassword) { QSqlQuery query; - query.prepare("UPDATE Users SET hashedPassword = :newHashedPassword, avatar = :newAvatar WHERE account = :account"); + query.prepare("UPDATE Users SET hashedPassword = :newHashedPassword WHERE account = :account"); query.bindValue(":newHashedPassword", newHashedPassword); - query.bindValue(":newAvatar", newAvatar); query.bindValue(":account", account); if (!query.exec()) { @@ -110,20 +108,3 @@ QPair DatabaseManager::getUserPassword(const QString &account) // 如果没有找到匹配的账号,返回空值 return userPassword; } - -QString DatabaseManager::getUserAvatar(const QString &account) -{ - QSqlQuery query; - query.prepare("SELECT avatar FROM Users WHERE account = :account"); - query.bindValue(":account", account); - - if (query.exec()) { - if (query.next()) { - return query.value(0).toString(); - } - } else { - qWarning() << "Failed to get user avatar:" << query.lastError(); - } - - return QString(); -} diff --git a/databasemanager.h b/databasemanager.h index 85cd742..bfb66b6 100644 --- a/databasemanager.h +++ b/databasemanager.h @@ -15,12 +15,11 @@ class DatabaseManager : public QObject QMapaccountPasswordMap_; public: explicit DatabaseManager(QObject *parent = nullptr); - bool updateUserInfo(const QString &account, const QString &newHashedPassword, const QString &newAvatar); + bool updateUserInfo(const QString &account, const QString &newHashedPassword); bool initializeDatabase(); - bool insertUser(const QString &account, const QString &hashedPassword, const QString &avatar = "E:\24SummerTraining\avatar_placeholder.png"); + bool insertUser(const QString &account, const QString &hashedPassword); QListgetAllAccounts(); QPairgetUserPassword(const QString &account); - QString getUserAvatar(const QString &account); }; #endif // DATABASEMANAGER_H