Fix bugs: Logically Fail to Login in when table is empty

This commit is contained in:
2025-04-22 18:57:48 +08:00
parent 262bd165e1
commit 27bbb5eb84
3 changed files with 32 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
#include "userAuth.h"
#include "tools.h"
#include <cstdlib>
const char *CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS user_data ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
@@ -33,6 +34,32 @@ void init_user_data(sqlite3 *db) {
};
};
bool is_user_table_empty(sqlite3 *db) {
const char *query = "SELECT COUNT(*) FROM user_data;";
sqlite3_stmt *stmt;
bool isEmpty = true;
// 准备SQL语句
if (sqlite3_prepare_v2(db, query, -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "Failed to prepare statement: " << sqlite3_errmsg(db)
<< std::endl;
return true; // 假设出错时表为空(根据你的需求可能需要调整)
}
// 执行查询
if (sqlite3_step(stmt) == SQLITE_ROW) {
int count = sqlite3_column_int(stmt, 0);
isEmpty = (count == 0);
} else {
std::cerr << "Failed to execute query: " << sqlite3_errmsg(db) << std::endl;
}
// 释放语句
sqlite3_finalize(stmt);
return isEmpty;
}
void register_user(sqlite3 *db, const string &username,
const string &userpassword) {
sqlite3_stmt *stmt;