From f9fe11d9dc549ce625b415f0127af06cb3881f29 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 28 Oct 2023 15:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E5=88=86=E9=A1=B5=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=EF=BC=88=E7=AC=AC=E4=B8=80=E6=AC=A1=E5=AE=9E=E9=AA=8C?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=EF=BC=8C=E6=B3=A8=E9=87=8A=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=BE=85=E8=A7=A3=E5=86=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cgi-bin/green_house_info_show.c | 55 +++++++++++++++++++++------------ html/green_house_info_show.html | 17 ++++++++++ 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/cgi-bin/green_house_info_show.c b/cgi-bin/green_house_info_show.c index 2bf02de..7fe0eb4 100644 --- a/cgi-bin/green_house_info_show.c +++ b/cgi-bin/green_house_info_show.c @@ -8,19 +8,23 @@ // sqlite3的回调函数 // sqlite 每查到一条记录,就调用一次这个回调 -int sql_search_callback(void* data, int argc, char** argv, char** azColName) { - cJSON* result_array = (cJSON*)data; - cJSON* item = cJSON_CreateObject(); +int sql_search_callback(void *data, int argc, char **argv, char **azColName) +{ + cJSON *result_array = (cJSON *)data; + cJSON *item = cJSON_CreateObject(); // 遍历每一列,并将数据添加到 JSON 对象中 - for (int i = 0; i < argc; i++) { + for (int i = 0; i < argc; i++) + { cJSON_AddStringToObject(item, azColName[i], argv[i] ? argv[i] : ""); } int isDuplicate = 0; - cJSON* existingItem = result_array->child; - while (existingItem != NULL) { - if (cJSON_Compare(existingItem, item, 1)) { + cJSON *existingItem = result_array->child; + while (existingItem != NULL) + { + if (cJSON_Compare(existingItem, item, 1)) + { isDuplicate = 1; break; } @@ -28,43 +32,56 @@ int sql_search_callback(void* data, int argc, char** argv, char** azColName) { } // 如果不是重复的结果,则添加到 JSON 数组中 - if (!isDuplicate) { + if (!isDuplicate) + { cJSON_AddItemToArray(result_array, item); } return 0; } -int main(int argc, char const* argv[]) { +int main(int argc, char const *argv[]) +{ // 连接到 SQLite 数据库 - sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象 - char* zErrMsg = 0; // 用于存储错误信息 - int rc; // 用于存储函数返回值 + sqlite3 *db; // 声明 sqlite3 类型变量,即声明一个数据库对象 + char *zErrMsg = 0; // 用于存储错误信息 + int rc; // 用于存储函数返回值 rc = sqlite3_open( "../sql_base/green_house.db", - &db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 - if (rc) { + &db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 + if (rc) + { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } fprintf(stderr, "ok Opened database successfully\n"); + // 解析分页参数 + int page = 1; // 默认第一页 + int limit = 10; // 默认每页10条 + if (getenv("QUERY_STRING")) + { + sscanf(getenv("QUERY_STRING"), "page=%d&limit=%d", &page, &limit); + } + // 构建查询语句 char query_sql[128] = ""; sprintf( query_sql, - "select temperature, humidity , th_date_time from temp_hum_info ;"); // 构建查询 + "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); // 构建查询 - cJSON* result_array = cJSON_CreateArray(); + cJSON *result_array = cJSON_CreateArray(); // printf("query_sql is %s\n", query_sql); // 执行查询语句 rc = sqlite3_exec(db, query_sql, sql_search_callback, result_array, - &zErrMsg); // 执行查询语句 + &zErrMsg); // 执行查询语句 // rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); // // 执行查询语句 - if (rc != SQLITE_OK) { + if (rc != SQLITE_OK) + { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); // cJSON_Delete(json_buf); @@ -72,7 +89,7 @@ int main(int argc, char const* argv[]) { return 1; } - char* json_str = cJSON_Print(result_array); + char *json_str = cJSON_Print(result_array); printf("Content-Type: application/json;charset=utf-8\r\n"); printf("\r\n"); diff --git a/html/green_house_info_show.html b/html/green_house_info_show.html index 41ae28b..5e457dd 100644 --- a/html/green_house_info_show.html +++ b/html/green_house_info_show.html @@ -66,11 +66,17 @@ +