From 9ddea88f8c1c3cac27dcc6a51b567af30714bf73 Mon Sep 17 00:00:00 2001 From: chang <15294300261@163.com> Date: Sat, 28 Oct 2023 14:26:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=93=8D=E4=BD=9C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cgi-bin/Makefile | 5 + cgi-bin/green_house_info_show.c | 2 +- cgi-bin/record_of_operation_history.c | 116 ----------------- cgi-bin/record_of_operations_history.c | 88 +++++++++++++ html/green_house_info_show.html | 5 +- html/index.html | 2 +- html/record_of_operations_history.html | 120 ++++++++++++------ sql_base/green_house.db | Bin 5120 -> 5120 bytes .../operations_history_insert_test_datas.sql | 14 ++ 9 files changed, 196 insertions(+), 156 deletions(-) delete mode 100644 cgi-bin/record_of_operation_history.c create mode 100644 cgi-bin/record_of_operations_history.c create mode 100644 sql_base/operations_history_insert_test_datas.sql diff --git a/cgi-bin/Makefile b/cgi-bin/Makefile index bf2f86c..33c7772 100644 --- a/cgi-bin/Makefile +++ b/cgi-bin/Makefile @@ -16,6 +16,11 @@ all: $(cc) sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) $(cc) sqlite3.c sqlite_cgi_insert_base.c -o sqlite_cgi_insert_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) $(cc) sqlite3.c green_house_info_show.c -o green_house_info_show.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) + $(cc) sqlite3.c record_of_operations_history.c -o record_of_operations_history.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) + +record_of_operations_history: + $(cc) sqlite3.c record_of_operations_history.c -o record_of_operations_history.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) + green_house_info_show: $(cc) sqlite3.c green_house_info_show.c -o green_house_info_show.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) diff --git a/cgi-bin/green_house_info_show.c b/cgi-bin/green_house_info_show.c index 6654285..2bf02de 100644 --- a/cgi-bin/green_house_info_show.c +++ b/cgi-bin/green_house_info_show.c @@ -53,7 +53,7 @@ int main(int argc, char const* argv[]) { 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 ;"); // 构建查询 cJSON* result_array = cJSON_CreateArray(); diff --git a/cgi-bin/record_of_operation_history.c b/cgi-bin/record_of_operation_history.c deleted file mode 100644 index 89773d8..0000000 --- a/cgi-bin/record_of_operation_history.c +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include -#include -#include "includes/sqlite3.h" - -// cJSON *result_json; // 用于存储查询结果 - -// sqlite3的回调函数 -// sqlite 每查到一条记录,就调用一次这个回调 -int sql_search_callback(void* NotUsed, int argc, char** argv, char** azColName); - -int main(int argc, char const* argv[]) { - // result_json = cJSON_CreateObject(); // 创建 JSON 对象 - - // 连接到 SQLite 数据库 - sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象 - char* zErrMsg = 0; // 用于存储错误信息 - int rc; // 用于存储函数返回值 - - rc = sqlite3_open( - "../sql_base/green_house.db", - &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"); - - // 获取前端传递的 JSON 数据 - char request_data[128] = ""; - fgets(request_data, 128, stdin); // 从标准输入中读取一行数据 - - // 解析 JSON 数据 - cJSON* json_buf = cJSON_Parse(request_data); // 解析 JSON 数据 - if (json_buf == NULL) { - fprintf(stderr, "Error parsing JSON data\n"); - sqlite3_close(db); - return 1; - } - - // 获取用户名和密码 - cJSON* user = cJSON_GetObjectItemCaseSensitive(json_buf, "name"); - cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password"); - - // if (user == NULL || password == NULL || !cJSON_IsString(user) || - // !cJSON_IsString(password)) { // 是否是非法输入 - // fprintf(stderr, - // "Error getting username and/or password from JSON data\n"); - // cJSON_Delete(json_buf); - // sqlite3_close(db); - // return 1; - // } - - // 拿到用户名和密码 - 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); - - // 构建查询语句 - char query_sql[128] = ""; - sprintf(query_sql, - "insert into record_of_operations " - "(oper_user_name,operation_detail,operation_time) " - "values('%s','%s','%s');", - username_from_frontend, password_from_frontend); // 构建插入语句 - - printf("query_sql is %s\n", query_sql); - - // 执行查询语句 - rc = sqlite3_exec(db, query_sql, NULL, NULL, &zErrMsg); // 执行查询语句 - // rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); // - // 执行查询语句 - if (rc != SQLITE_OK) { - fprintf(stderr, "SQL error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - cJSON_Delete(json_buf); - sqlite3_close(db); - return 1; - } - - // 释放资源 - cJSON_Delete(json_buf); - sqlite3_close(db); - - return 0; -} - -int sql_search_callback(void* NotUsed, - int argc, - char** argv, - char** azColName) { - // int i; - // for (i = 0; i < argc; i++) - // { - // printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); - // } - cJSON* result_json = cJSON_CreateObject(); - if (argc == 0) { - cJSON_AddNumberToObject(result_json, "code", 1); - cJSON_AddStringToObject(result_json, "msg", "用户名或密码错误"); - } else - cJSON_AddNumberToObject(result_json, "code", 0); - - char* result_json_string = cJSON_Print(result_json); - - // 输出 JSON 结果 - printf("content-type: application/json;charset=utf-8\r\n"); - printf("\r\n"); - printf("%s\n", result_json_string); - - free(result_json_string); - return SQLITE_OK; -} \ No newline at end of file diff --git a/cgi-bin/record_of_operations_history.c b/cgi-bin/record_of_operations_history.c new file mode 100644 index 0000000..8beee6d --- /dev/null +++ b/cgi-bin/record_of_operations_history.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include "includes/sqlite3.h" + +// cJSON *result_json; // 用于存储查询结果 + +// sqlite3的回调函数 +// sqlite 每查到一条记录,就调用一次这个回调 +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++) { + 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)) { + isDuplicate = 1; + break; + } + existingItem = existingItem->next; + } + + // 如果不是重复的结果,则添加到 JSON 数组中 + if (!isDuplicate) { + cJSON_AddItemToArray(result_array, item); + } + return 0; +} +int main(int argc, char const* argv[]) { + // 连接到 SQLite 数据库 + sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象 + char* zErrMsg = 0; // 用于存储错误信息 + int rc; // 用于存储函数返回值 + + rc = sqlite3_open( + "../sql_base/green_house.db", + &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"); + + // 构建查询语句 + char query_sql[128] = ""; + sprintf(query_sql, + "select oper_user_name, operation_detail , operation_time from " + "record_of_operations ;"); // 构建查询 + + 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); // 执行查询语句 + // rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); // + // 执行查询语句 + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + // cJSON_Delete(json_buf); + sqlite3_close(db); + return 1; + } + + char* json_str = cJSON_Print(result_array); + + 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; +} diff --git a/html/green_house_info_show.html b/html/green_house_info_show.html index 4c86851..41ae28b 100644 --- a/html/green_house_info_show.html +++ b/html/green_house_info_show.html @@ -22,6 +22,7 @@ border-collapse: collapse; width: 100%; /* width: 700px; */ + height: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } @@ -38,7 +39,7 @@ background-color: #f0f0f0; } - tr{ + tr { height: 10px; } @@ -96,4 +97,4 @@ - + \ No newline at end of file diff --git a/html/index.html b/html/index.html index 5d9af59..ad802ed 100644 --- a/html/index.html +++ b/html/index.html @@ -143,7 +143,7 @@ 历史操作
- +
diff --git a/html/record_of_operations_history.html b/html/record_of_operations_history.html index 46c4725..3df2f76 100644 --- a/html/record_of_operations_history.html +++ b/html/record_of_operations_history.html @@ -1,50 +1,98 @@ - + - - - 历史操作表 + + + 温湿度 + -

历史操作表

- - - - - +
+
用户名操作指令操作时间
+ + + + + + - - - - -
用户操作时间
+ + + diff --git a/sql_base/green_house.db b/sql_base/green_house.db index d688d2da0059028c2dbe59fce7e1e24e3b40c5cc..d4d33ec9722585d02d2ab496ff07027e2b81eb31 100644 GIT binary patch delta 167 zcmZqBXwaA-&8RR@#+gxJW5N<<5f0`q2IddU516koA7b9Yyoz}abJu1?mLg^a0S*=h zNy%s1I-WJIhmbt%EDWNe6;LpF6?+s=`xXY~OUx&jzcasOzQz2Mc|Y@(&59gzm_@jl zB^e>wI5?O^89|!ZH=p3PVPRure#OB2YO|og17SFuL{Wlk_K kUt&JN{GItN^DXA5K$$Jfn@@1turM*O0690ACr(fV01e(60RR91 diff --git a/sql_base/operations_history_insert_test_datas.sql b/sql_base/operations_history_insert_test_datas.sql new file mode 100644 index 0000000..c647dac --- /dev/null +++ b/sql_base/operations_history_insert_test_datas.sql @@ -0,0 +1,14 @@ +-- 开启表头显示 +.headers on + +-- 设置列的宽度 +.mode column + +-- 插入数据到用户登录表 +insert into record_of_operations + (oper_user_name,operation_detail, operation_time) +values + ('admin', 'led:on', 'null'); + +-- 显示用户登录表的数据 +select * from record_of_operations; \ No newline at end of file