63 lines
1.8 KiB
C
63 lines
1.8 KiB
C
//
|
|
// Created by zhang on 2024/12/29.
|
|
//
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "./include/function.h"
|
|
#include "./include/sqlite3.h"
|
|
|
|
void Signin(const char *username,const char*password,int *recall,char *errmsg){
|
|
sqlite3 *db;
|
|
char *errMsg = 0;
|
|
int rc;
|
|
// 打开数据库,如果数据库文件不存在则创建它
|
|
rc = sqlite3_open("./data/msg.db", &db);
|
|
if (rc) {
|
|
strcpy(errmsg,"Error:database error.");
|
|
*recall=-1;
|
|
return;
|
|
}
|
|
const char *sql_create_table =
|
|
"CREATE TABLE IF NOT EXISTS users "
|
|
"("
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
"user TEXT UNIQUE,"
|
|
"password TEXT"
|
|
");";
|
|
|
|
// 执行 SQL 语句
|
|
rc = sqlite3_exec(db, sql_create_table, 0, 0, &errMsg);
|
|
if (rc != SQLITE_OK) {
|
|
strcpy(errmsg,"Error:database error.");
|
|
sqlite3_free(errMsg);
|
|
sqlite3_close(db);
|
|
*recall=-1;return;
|
|
}
|
|
const char *sql_insert="INSERT INTO users (user, password) VALUES (?, ?);";
|
|
sqlite3_stmt *insert;
|
|
rc = sqlite3_prepare_v2(db, sql_insert, -1, &insert, 0);
|
|
if (rc != SQLITE_OK) {
|
|
strcpy(errmsg,"Error:database error.");
|
|
*recall=-1;
|
|
sqlite3_close(db);
|
|
return;
|
|
}
|
|
|
|
// 将第一个参数 (name) 绑定到 SQL 语句中的第一个 "?"
|
|
sqlite3_bind_text(insert, 1, username, -1, SQLITE_STATIC);
|
|
|
|
// 将第二个参数 (email) 绑定到 SQL 语句中的第二个 "?"
|
|
sqlite3_bind_text(insert, 2, password, -1, SQLITE_STATIC);
|
|
|
|
rc = sqlite3_exec(db,insert,0,0,&errMsg);
|
|
if (rc != SQLITE_OK) {
|
|
strcpy(errmsg,errMsg);
|
|
sqlite3_free(errMsg);
|
|
sqlite3_finalize(insert);
|
|
sqlite3_close(db);
|
|
*recall=-1;return;
|
|
}
|
|
sqlite3_close(db);
|
|
*recall=0;
|
|
return;
|
|
} |