2023-10-24 17:46:11 +08:00
|
|
|
#include <cjson/cJSON.h>
|
2023-10-25 10:47:08 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
// #include "cJSON.h"
|
|
|
|
#include "sqlite3.h"
|
2023-10-24 17:46:11 +08:00
|
|
|
|
2023-10-25 14:04:49 +08:00
|
|
|
// sqlite3的回调函数
|
|
|
|
// sqlite 每查到一条记录,就调用一次这个回调
|
|
|
|
int sql_search_callback(void* NotUsed, int argc, char** argv, char** azColName);
|
|
|
|
|
2023-10-25 10:47:08 +08:00
|
|
|
int main(int argc, char const* argv[]) {
|
2023-10-24 17:46:11 +08:00
|
|
|
// 连接到 SQLite 数据库
|
2023-10-25 10:47:08 +08:00
|
|
|
sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
|
|
|
char* zErrMsg = 0; // 用于存储错误信息
|
|
|
|
int rc; // 用于存储函数返回值
|
2023-10-24 17:46:11 +08:00
|
|
|
|
2023-10-25 10:47:08 +08:00
|
|
|
rc = sqlite3_open(
|
|
|
|
"../sql_base/green_house.db",
|
|
|
|
&db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
|
|
|
|
if (rc) {
|
2023-10-24 17:46:11 +08:00
|
|
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-10-25 10:47:08 +08:00
|
|
|
fprintf(stderr, "ok Opened database successfully\n");
|
2023-10-24 17:46:11 +08:00
|
|
|
|
|
|
|
// 获取前端传递的 JSON 数据
|
|
|
|
char request_data[128] = "";
|
2023-10-25 10:47:08 +08:00
|
|
|
fgets(request_data, 128, stdin); // 从标准输入中读取一行数据
|
2023-10-24 17:46:11 +08:00
|
|
|
|
|
|
|
// 解析 JSON 数据
|
2023-10-25 10:47:08 +08:00
|
|
|
cJSON* json_buf = cJSON_Parse(request_data); // 解析 JSON 数据
|
|
|
|
if (json_buf == NULL) {
|
2023-10-24 17:46:11 +08:00
|
|
|
fprintf(stderr, "Error parsing JSON data\n");
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取用户名和密码
|
2023-10-25 10:47:08 +08:00
|
|
|
cJSON* user = cJSON_GetObjectItemCaseSensitive(json_buf, "name");
|
|
|
|
cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
|
2023-10-24 17:46:11 +08:00
|
|
|
|
2023-10-25 10:47:08 +08:00
|
|
|
if (user == NULL || password == NULL || !cJSON_IsString(user) ||
|
2023-10-25 14:04:49 +08:00
|
|
|
!cJSON_IsString(password)) { // 是否是非法输入
|
2023-10-25 10:47:08 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Error getting username and/or password from JSON data\n");
|
2023-10-24 17:46:11 +08:00
|
|
|
cJSON_Delete(json_buf);
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 拿到用户名和密码
|
2023-10-25 10:47:08 +08:00
|
|
|
char* username_from_frontend = user->valuestring;
|
|
|
|
char* password_from_frontend = password->valuestring;
|
|
|
|
|
|
|
|
printf("username is %s, pwd is %s\n", username_from_frontend,
|
|
|
|
password_from_frontend);
|
2023-10-24 17:46:11 +08:00
|
|
|
|
|
|
|
// 构建查询语句
|
|
|
|
char query_sql[128] = "";
|
2023-10-25 10:47:08 +08:00
|
|
|
sprintf(query_sql,
|
|
|
|
"select * from login_users where username='%s' and password='%s'",
|
|
|
|
username_from_frontend, password_from_frontend); // 构建查询语句
|
2023-10-24 17:46:11 +08:00
|
|
|
|
|
|
|
// 执行查询语句
|
2023-10-25 14:04:49 +08:00
|
|
|
// rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句
|
|
|
|
rc = sqlite3_exec(db, query_sql, sql_search_callback, 0,
|
|
|
|
&zErrMsg); // 执行查询语句
|
2023-10-25 10:47:08 +08:00
|
|
|
if (rc != SQLITE_OK) {
|
2023-10-24 17:46:11 +08:00
|
|
|
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
|
|
|
sqlite3_free(zErrMsg);
|
|
|
|
cJSON_Delete(json_buf);
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 构建 JSON 数据
|
2023-10-25 10:47:08 +08:00
|
|
|
cJSON* result_json = cJSON_CreateObject();
|
2023-10-24 17:46:11 +08:00
|
|
|
cJSON_AddItemToObject(result_json, "result", cJSON_CreateString("success"));
|
2023-10-25 10:47:08 +08:00
|
|
|
cJSON_AddItemToObject(result_json, "username",
|
|
|
|
cJSON_CreateString(username_from_frontend));
|
2023-10-24 17:46:11 +08:00
|
|
|
|
|
|
|
// 打印 JSON 数据
|
2023-10-25 10:47:08 +08:00
|
|
|
char* result_json_str = cJSON_Print(result_json);
|
2023-10-24 17:46:11 +08:00
|
|
|
printf("%s\n", result_json_str);
|
|
|
|
|
|
|
|
// 释放资源
|
|
|
|
cJSON_Delete(json_buf);
|
|
|
|
cJSON_Delete(result_json);
|
|
|
|
sqlite3_close(db);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-10-25 14:04:49 +08:00
|
|
|
|
|
|
|
int sql_search_callback(void* NotUsed,
|
|
|
|
int argc,
|
|
|
|
char** argv,
|
|
|
|
char** azColName) {}
|