indicatorTree-v10练习(有问题)

ops/2024/10/17 20:30:38/

目标:设计数据库表表格式,将“indicatorTree-v10.json”导入到数据库,再从数据库读取写为JSON文件。

其他要求:数据库要求为mysql数据库;编程语言暂时限定为C;JSON解析使用本文件夹中的cJSON.c和cJSON.h;代码编写风格严格按照“TDYTH编程规范_20220527_C.doc”。

提示:可使用递归写法,也可以自己构建栈结构。gcc编译的Makefile文件参考“Makefile参考”。

数据库表设计

CREATE TABLE `Indicator` (`id` varchar(10) NOT NULL,`abilityName` varchar(255) DEFAULT NULL,`level` int DEFAULT NULL,`defaultWeight` float DEFAULT NULL,`area` text,`devType` text,`technologicalSystem` text,`enable` tinyint(1) DEFAULT NULL,`parent_id` varchar(10) DEFAULT NULL,PRIMARY KEY (`id`),KEY `parent_id` (`parent_id`),CONSTRAINT `Indicator_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `Indicator` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;CREATE TABLE `CalculateParams` (`id` int NOT NULL AUTO_INCREMENT,`indicator_id` varchar(10) DEFAULT NULL,`param_key` varchar(255) DEFAULT NULL,`param_value` text,PRIMARY KEY (`id`),KEY `indicator_id` (`indicator_id`),CONSTRAINT `CalculateParams_ibfk_1` FOREIGN KEY (`indicator_id`) REFERENCES `Indicator` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;CREATE TABLE `IndicatorSystem` (`id` varchar(10) NOT NULL,`name` varchar(255) DEFAULT NULL,`description` text,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

C 代码示例

使用cJSON库来解析JSON文件并将数据插入到数据库中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include "cJSON.h"// 插入 Indicator 表
void insert_indicator(cJSON *indicator, MYSQL *conn, const char *parent_id) {cJSON *id = cJSON_GetObjectItem(indicator, "id");cJSON *abilityName = cJSON_GetObjectItem(indicator, "abilityName");cJSON *level = cJSON_GetObjectItem(indicator, "level");cJSON *defaultWeight = cJSON_GetObjectItem(indicator, "defaultWeight");cJSON *enable = cJSON_GetObjectItem(indicator, "enable");char query[2048];char parent_id_str[64];if (parent_id) {snprintf(parent_id_str, sizeof(parent_id_str), "'%s'", parent_id);} else {snprintf(parent_id_str, sizeof(parent_id_str), "NULL");}snprintf(query, sizeof(query),"INSERT INTO Indicator (id, abilityName, level, defaultWeight, enable, parent_id) ""VALUES ('%s', '%s', %d, %f, %d, %s) ""ON DUPLICATE KEY UPDATE abilityName=VALUES(abilityName), level=VALUES(level), ""defaultWeight=VALUES(defaultWeight), enable=VALUES(enable), parent_id=VALUES(parent_id)",cJSON_GetStringValue(id), cJSON_GetStringValue(abilityName), level->valueint,defaultWeight->valuedouble, enable->valueint, parent_id_str);printf("Executing query: %s\n", query);if (mysql_query(conn, query)) {fprintf(stderr, "Insert error: %s\n", mysql_error(conn));exit(1);}cJSON *children = cJSON_GetObjectItem(indicator, "indicator");if (cJSON_IsArray(children)) {cJSON *child;cJSON_ArrayForEach(child, children) {insert_indicator(child, conn, cJSON_GetStringValue(id));}}
}// 插入 CalculateParams 表
void insert_calculate_params(cJSON *params, MYSQL *conn) {cJSON *id = cJSON_GetObjectItem(params, "id");cJSON *indicator_id = cJSON_GetObjectItem(params, "indicator_id");cJSON *param_key = cJSON_GetObjectItem(params, "param_key");cJSON *param_value = cJSON_GetObjectItem(params, "param_value");char query[1024];snprintf(query, sizeof(query),"INSERT INTO CalculateParams (id, indicator_id, param_key, param_value) ""VALUES ('%s', '%s', '%s', '%s') ""ON DUPLICATE KEY UPDATE param_key=VALUES(param_key), param_value=VALUES(param_value)",cJSON_GetStringValue(id), cJSON_GetStringValue(indicator_id),cJSON_GetStringValue(param_key), cJSON_GetStringValue(param_value));if (mysql_query(conn, query)) {fprintf(stderr, "Insert error: %s\n", mysql_error(conn));exit(1);}
}// 插入 IndicatorSystem 表
void insert_indicator_system(cJSON *system, MYSQL *conn) {cJSON *id = cJSON_GetObjectItem(system, "id");cJSON *name = cJSON_GetObjectItem(system, "name");cJSON *description = cJSON_GetObjectItem(system, "description");char query[1024];snprintf(query, sizeof(query),"INSERT INTO IndicatorSystem (id, name, description) ""VALUES ('%s', '%s', '%s') ""ON DUPLICATE KEY UPDATE name=VALUES(name), description=VALUES(description)",cJSON_GetStringValue(id), cJSON_GetStringValue(name), cJSON_GetStringValue(description));if (mysql_query(conn, query)) {fprintf(stderr, "Insert error: %s\n", mysql_error(conn));exit(1);}
}// 从数据库中获取 Indicator 数据
cJSON* fetch_indicators(MYSQL *conn, const char *parent_id) {char query[1024];snprintf(query, sizeof(query), "SELECT id, abilityName, level, defaultWeight, enable FROM Indicator WHERE parent_id %s",parent_id ? "= '" : "IS NULL");if (parent_id) {strcat(query, parent_id);strcat(query, "'");}if (mysql_query(conn, query)) {fprintf(stderr, "Select error: %s\n", mysql_error(conn));exit(1);}MYSQL_RES *result = mysql_store_result(conn);if (result == NULL) {fprintf(stderr, "mysql_store_result() failed\n");exit(1);}MYSQL_ROW row;cJSON *json_array = cJSON_CreateArray();while ((row = mysql_fetch_row(result))) {cJSON *indicator = cJSON_CreateObject();cJSON_AddStringToObject(indicator, "id", row[0]);cJSON_AddStringToObject(indicator, "abilityName", row[1]);cJSON_AddNumberToObject(indicator, "level", atoi(row[2]));cJSON_AddNumberToObject(indicator, "defaultWeight", atof(row[3]));cJSON_AddBoolToObject(indicator, "enable", atoi(row[4]));cJSON *children = fetch_indicators(conn, row[0]);cJSON_AddItemToObject(indicator, "indicator", children);cJSON_AddItemToArray(json_array, indicator);}mysql_free_result(result);return json_array;
}int main() {MYSQL *conn = mysql_init(NULL);if (conn == NULL) {fprintf(stderr, "mysql_init() failed\n");return EXIT_FAILURE;}if (mysql_real_connect(conn, "192.168.84.1", "root", "***", "***", 0, NULL, 0) == NULL) {fprintf(stderr, "mysql_real_connect() failed\n");mysql_close(conn);return EXIT_FAILURE;}FILE *file = fopen("indicatorTree-v10.json", "r");if (!file) {fprintf(stderr, "Could not open file\n");return EXIT_FAILURE;}fseek(file, 0, SEEK_END);long length = ftell(file);fseek(file, 0, SEEK_SET);char *data = malloc(length);if (data == NULL) {fprintf(stderr, "Memory allocation failed\n");return EXIT_FAILURE;}fread(data, 1, length, file);fclose(file);cJSON *json = cJSON_Parse(data);if (!json) {fprintf(stderr, "Error parsing JSON\n");free(data);return EXIT_FAILURE;}cJSON *indicatorList = cJSON_GetObjectItem(json, "indicator");if (cJSON_IsArray(indicatorList)) {cJSON *indicator;cJSON_ArrayForEach(indicator, indicatorList) {insert_indicator(indicator, conn, NULL);}}cJSON *calculateParamsList = cJSON_GetObjectItem(json, "calculateParams");if (cJSON_IsArray(calculateParamsList)) {cJSON *param;cJSON_ArrayForEach(param, calculateParamsList) {insert_calculate_params(param, conn);}}cJSON *indicatorSystemsList = cJSON_GetObjectItem(json, "indicatorSystems");if (cJSON_IsArray(indicatorSystemsList)) {cJSON *system;cJSON_ArrayForEach(system, indicatorSystemsList) {insert_indicator_system(system, conn);}}free(data);cJSON_Delete(json);cJSON *json_array = fetch_indicators(conn, NULL);char *json_string = cJSON_Print(json_array);printf("%s\n", json_string);cJSON_Delete(json_array);free(json_string);mysql_close(conn);return EXIT_SUCCESS;
}

http://www.ppmy.cn/ops/126291.html

相关文章

Flink-运行架构

flink运行架构涉及到四大组件&#xff1a; 作业管理器&#xff08;JobManager&#xff09; 主要作用&#xff1a;是应用程序执行的主进程&#xff0c;换句话说&#xff0c;每一个flink进程都有一个对应的JobManager 所控制&#xff1b;JobManager会接收 应用程序所需要的可执行…

前端文件流导出

1、前端代码 ​ /** 导出 */ const handleExport async () > {let config {responseType: blob,headers: {Content-Type: application/json,},};const res await getTargetExport(config);const blob new Blob([res]);const fileName PK目标跟进导出列表.xls;const li…

环境变量(Linux)

文章目录 一、什么是环境变量&#xff1f;二、环境变量的作用1. 方便命令执行&#xff1a;2.配置系统和应用程序&#xff1a;3.用户自定义环境变量&#xff1a; 三、Linux 常见环境变量四、设置环境变量1.临时设置&#xff1a;2.永久设置&#xff1a; 五、环境变量的优先级六、…

国际期货收费行情源CTP推送式/期货配资软件开发对接行情源的技术性说明

在现代金融市场中&#xff0c;期货交易因其高风险和高回报特性而备受关注。为了满足期货交易者的需求&#xff0c;开发高效、稳定和安全的期货交易软件变得尤为重要。本文将对国际期货收费行情源CTP推送式及期货配资软件的开发对接行情源的技术细节进行详细说明。 一、CTP&…

抖音小游戏画图位置移动

文章目录 画图移动图形位置 画图 const canvas tt.createCanvas(); const context canvas.getContext(2d);context.width 500; context.height 500;let isPressing false; // 是否按下 let startX 0; let startY 0;context.fillStyle "#f00"; context.fillR…

js-前端vue强制刷新当前页面(router.go(0) window.location.方法)

1.强制刷新当前页面 在完成某一操作后需要刷新当前整体页面&#xff0c;或者部分模块需要重新渲染数据。强制刷新页面可能会影响用户体验&#xff0c;特别是在需要保持页面状态的情况下。使用强制刷新页面需要谨慎使用。 2.方法 2.1 Vue Router 的 router.go(0) 方法&#x…

sealed class-kotlin中的封闭类

在 Kotlin 中&#xff0c;sealed class&#xff08;密封类&#xff09;是一种特殊的类&#xff0c;用于限制继承的类的数量。密封类可以被用来表示一组有限的类型&#xff0c;通常用于状态管理或表达多种可能的错误类型。 密封类用 sealed 关键字定义&#xff0c;这意味着只能…

C++游戏开发完整学习路径

C游戏开发完整学习路径 引言 随着游戏行业的迅速发展&#xff0c;C作为主要的游戏开发语言&#xff0c;因其高效性和灵活性&#xff0c;依然受到广泛欢迎。C不仅在大型游戏开发中被广泛使用&#xff0c;而且在游戏引擎的构建、性能优化和复杂算法的实现中也扮演着关键角色。本…