math/login.c

57 lines
1.4 KiB
C

#include <stdio.h>
#include<string.h>
#include "./include/function.h"
#include "./include/ui.h"
void Login(char *username,char*password,int *recall,char *errmsg){
sqlite3 *db;
int rc;
// 打开数据库
rc = sqlite3_open("./data/msg.db", &db);
if (rc) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
return;
}
// SQL 查询语句:查找用户及其密码
const char *sql = "SELECT user, password FROM users WHERE user = ?;";
sqlite3_stmt *stmt;
// 编译 SQL 语句
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
strcpy(errmsg,"Error:database error.");
*recall=-1;
sqlite3_close(db);
return;
}
// 绑定查询参数
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
// 查找到用户,获取密码
const char *db_password = (const char *)sqlite3_column_text(stmt, 1);
// 如果密码匹配
if (db_password && strcmp(db_password, password) == 0) {
*recall=0;
} else {
strcpy(errmsg,"Incorrect username or password.");
*recall=-1;
}
} else {
strcpy(errmsg,"Incorrect username or password.");
*recall=-1;
}
// 绑定查询参数
// 清理资源
sqlite3_finalize(stmt);
sqlite3_close(db);
return;
}