修复了cjson的bug
This commit is contained in:
parent
dd66f9b1c6
commit
2c30d489b2
|
@ -1,7 +1,7 @@
|
||||||
all:
|
all:
|
||||||
|
|
||||||
sqlite_cgi_base:
|
sqlite_cgi_base:
|
||||||
gcc sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base -lpthread -ldl
|
gcc sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base -lpthread -ldl -lcjson
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf sqlite_cgi_base
|
rm -rf sqlite_cgi_base
|
Binary file not shown.
|
@ -1,60 +1,65 @@
|
||||||
#include "sqlite3.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <cjson/cJSON.h>
|
#include <cjson/cJSON.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
// #include "cJSON.h"
|
||||||
|
#include "sqlite3.h"
|
||||||
|
|
||||||
int main(int argc, char const *argv[])
|
int main(int argc, char const* argv[]) {
|
||||||
{
|
|
||||||
// 连接到 SQLite 数据库
|
// 连接到 SQLite 数据库
|
||||||
sqlite3 *db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
||||||
char *zErrMsg = 0; // 用于存储错误信息
|
char* zErrMsg = 0; // 用于存储错误信息
|
||||||
int rc; // 用于存储函数返回值
|
int rc; // 用于存储函数返回值
|
||||||
|
|
||||||
rc = sqlite3_open("../sql_base/green_house.db", &db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
|
rc = sqlite3_open(
|
||||||
if (rc)
|
"../sql_base/green_house.db",
|
||||||
{
|
&db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
|
||||||
|
if (rc) {
|
||||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "好 Opened database successfully\n");
|
fprintf(stderr, "ok Opened database successfully\n");
|
||||||
|
|
||||||
// 获取前端传递的 JSON 数据
|
// 获取前端传递的 JSON 数据
|
||||||
char request_data[128] = "";
|
char request_data[128] = "";
|
||||||
fgets(request_data, 128, stdin); // 从标准输入中读取一行数据
|
fgets(request_data, 128, stdin); // 从标准输入中读取一行数据
|
||||||
|
|
||||||
// 解析 JSON 数据
|
// 解析 JSON 数据
|
||||||
cJSON *json_buf = cJSON_parse(request_data); // 解析 JSON 数据
|
cJSON* json_buf = cJSON_Parse(request_data); // 解析 JSON 数据
|
||||||
if (json_buf == NULL)
|
if (json_buf == NULL) {
|
||||||
{
|
|
||||||
fprintf(stderr, "Error parsing JSON data\n");
|
fprintf(stderr, "Error parsing JSON data\n");
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取用户名和密码
|
// 获取用户名和密码
|
||||||
cJSON *user = cJSON_GetObjectItemCaseSensitive(json_buf, "name");
|
cJSON* user = cJSON_GetObjectItemCaseSensitive(json_buf, "name");
|
||||||
cJSON *password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
|
cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
|
||||||
|
|
||||||
if (user == NULL || password == NULL || !cJSON_IsString(user) || !cJSON_IsString(password))
|
if (user == NULL || password == NULL || !cJSON_IsString(user) ||
|
||||||
{
|
!cJSON_IsString(password)) {
|
||||||
fprintf(stderr, "Error getting username and/or password from JSON data\n");
|
fprintf(stderr,
|
||||||
|
"Error getting username and/or password from JSON data\n");
|
||||||
cJSON_Delete(json_buf);
|
cJSON_Delete(json_buf);
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拿到用户名和密码
|
// 拿到用户名和密码
|
||||||
char *username_from_frontend = user->valuestring;
|
char* username_from_frontend = user->valuestring;
|
||||||
char *password_from_frontend = password->valuestring;
|
char* password_from_frontend = password->valuestring;
|
||||||
|
|
||||||
|
printf("username is %s, pwd is %s\n", username_from_frontend,
|
||||||
|
password_from_frontend);
|
||||||
|
|
||||||
// 构建查询语句
|
// 构建查询语句
|
||||||
char query_sql[128] = "";
|
char query_sql[128] = "";
|
||||||
sprintf(query_sql, "select * from login_users where username='%s' and password='%s'", username_from_frontend, password_from_frontend); // 构建查询语句
|
sprintf(query_sql,
|
||||||
|
"select * from login_users where username='%s' and password='%s'",
|
||||||
|
username_from_frontend, password_from_frontend); // 构建查询语句
|
||||||
|
|
||||||
// 执行查询语句
|
// 执行查询语句
|
||||||
rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句
|
rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句
|
||||||
if (rc != SQLITE_OK)
|
if (rc != SQLITE_OK) {
|
||||||
{
|
|
||||||
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
||||||
sqlite3_free(zErrMsg);
|
sqlite3_free(zErrMsg);
|
||||||
cJSON_Delete(json_buf);
|
cJSON_Delete(json_buf);
|
||||||
|
@ -63,12 +68,13 @@ int main(int argc, char const *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建 JSON 数据
|
// 构建 JSON 数据
|
||||||
cJSON *result_json = cJSON_CreateObject();
|
cJSON* result_json = cJSON_CreateObject();
|
||||||
cJSON_AddItemToObject(result_json, "result", cJSON_CreateString("success"));
|
cJSON_AddItemToObject(result_json, "result", cJSON_CreateString("success"));
|
||||||
cJSON_AddItemToObject(result_json, "username", cJSON_CreateString(username_from_frontend));
|
cJSON_AddItemToObject(result_json, "username",
|
||||||
|
cJSON_CreateString(username_from_frontend));
|
||||||
|
|
||||||
// 打印 JSON 数据
|
// 打印 JSON 数据
|
||||||
char *result_json_str = cJSON_Print(result_json);
|
char* result_json_str = cJSON_Print(result_json);
|
||||||
printf("%s\n", result_json_str);
|
printf("%s\n", result_json_str);
|
||||||
|
|
||||||
// 释放资源
|
// 释放资源
|
||||||
|
|
Loading…
Reference in New Issue