2023-10-27 11:53:48 +08:00
|
|
|
#include <cjson/cJSON.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "includes/sqlite3.h"
|
|
|
|
|
|
|
|
// cJSON *result_json; // 用于存储查询结果
|
|
|
|
|
|
|
|
// sqlite3的回调函数
|
|
|
|
// sqlite 每查到一条记录,就调用一次这个回调
|
2023-10-28 15:20:52 +08:00
|
|
|
int sql_search_callback(void *data, int argc, char **argv, char **azColName)
|
|
|
|
{
|
|
|
|
cJSON *result_array = (cJSON *)data;
|
|
|
|
cJSON *item = cJSON_CreateObject();
|
2023-10-27 11:53:48 +08:00
|
|
|
|
|
|
|
// 遍历每一列,并将数据添加到 JSON 对象中
|
2023-10-28 15:20:52 +08:00
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
cJSON_AddStringToObject(item, azColName[i], argv[i] ? argv[i] : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
int isDuplicate = 0;
|
2023-10-28 15:20:52 +08:00
|
|
|
cJSON *existingItem = result_array->child;
|
|
|
|
while (existingItem != NULL)
|
|
|
|
{
|
|
|
|
if (cJSON_Compare(existingItem, item, 1))
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
isDuplicate = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
existingItem = existingItem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果不是重复的结果,则添加到 JSON 数组中
|
2023-10-28 15:20:52 +08:00
|
|
|
if (!isDuplicate)
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
cJSON_AddItemToArray(result_array, item);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-10-28 15:20:52 +08:00
|
|
|
int main(int argc, char const *argv[])
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
// 连接到 SQLite 数据库
|
2023-10-28 15:20:52 +08:00
|
|
|
sqlite3 *db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
|
|
|
char *zErrMsg = 0; // 用于存储错误信息
|
|
|
|
int rc; // 用于存储函数返回值
|
2023-10-27 11:53:48 +08:00
|
|
|
|
|
|
|
rc = sqlite3_open(
|
|
|
|
"../sql_base/green_house.db",
|
2023-10-28 15:20:52 +08:00
|
|
|
&db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
|
|
|
|
if (rc)
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "ok Opened database successfully\n");
|
|
|
|
|
2023-10-28 15:20:52 +08:00
|
|
|
// 解析分页参数
|
|
|
|
int page = 1; // 默认第一页
|
|
|
|
int limit = 10; // 默认每页10条
|
|
|
|
if (getenv("QUERY_STRING"))
|
|
|
|
{
|
|
|
|
sscanf(getenv("QUERY_STRING"), "page=%d&limit=%d", &page, &limit);
|
|
|
|
}
|
|
|
|
|
2023-10-27 11:53:48 +08:00
|
|
|
// 构建查询语句
|
|
|
|
char query_sql[128] = "";
|
|
|
|
sprintf(
|
|
|
|
query_sql,
|
2023-10-28 15:29:52 +08:00
|
|
|
// "select temperature, humidity , th_date_time from temp_hum_info;"); // 构建查询
|
|
|
|
"select temperature, humidity , th_date_time from temp_hum_info ORDER BY thid DESC LIMIT %d OFFSET %d;", limit, (page - 1) * limit);
|
2023-10-27 11:53:48 +08:00
|
|
|
|
2023-10-28 15:20:52 +08:00
|
|
|
cJSON *result_array = cJSON_CreateArray();
|
2023-10-27 11:53:48 +08:00
|
|
|
|
|
|
|
// printf("query_sql is %s\n", query_sql);
|
|
|
|
|
|
|
|
// 执行查询语句
|
|
|
|
rc = sqlite3_exec(db, query_sql, sql_search_callback, result_array,
|
2023-10-28 15:20:52 +08:00
|
|
|
&zErrMsg); // 执行查询语句
|
2023-10-27 11:53:48 +08:00
|
|
|
// rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); //
|
|
|
|
// 执行查询语句
|
2023-10-28 15:20:52 +08:00
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
{
|
2023-10-27 11:53:48 +08:00
|
|
|
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
|
|
|
sqlite3_free(zErrMsg);
|
|
|
|
// cJSON_Delete(json_buf);
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-28 15:20:52 +08:00
|
|
|
char *json_str = cJSON_Print(result_array);
|
2023-10-27 11:53:48 +08:00
|
|
|
|
|
|
|
printf("Content-Type: application/json;charset=utf-8\r\n");
|
|
|
|
printf("\r\n");
|
|
|
|
printf("%s", json_str);
|
|
|
|
|
|
|
|
free(json_str);
|
|
|
|
cJSON_Delete(result_array);
|
|
|
|
|
|
|
|
// 释放资源
|
|
|
|
sqlite3_close(db);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|