C語言讀取data.json文件并存入MySQL數(shù)據(jù)庫小案例(推薦)
本地有一個(gè)data.json文件
data.json
[
{
"id": 1,
"name": "Alice",
"age": 30
},
{
"id": 2,
"name": "Bob",
"age": 25
}
]
要將 data.json 文件中的數(shù)據(jù)存儲(chǔ)到 MySQL 數(shù)據(jù)庫中,首先需要?jiǎng)?chuàng)建一個(gè)相應(yīng)的數(shù)據(jù)庫表。然后,你可以使用 C 語言和 cJSON 庫來讀取 JSON 文件并將數(shù)據(jù)插入到數(shù)據(jù)庫中。
1. 創(chuàng)建 MySQL 表
首先,在 MySQL 中創(chuàng)建一個(gè)表來存儲(chǔ)數(shù)據(jù)??梢允褂靡韵?SQL 語句:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
);

2. C 語言代碼示例
以下是一個(gè)完整的 C 語言示例,使用 cJSON 庫讀取 data.json 文件并將數(shù)據(jù)插入到 MySQL 數(shù)據(jù)庫中。
依賴項(xiàng)
確保你已經(jīng)安裝了以下庫:
- MySQL C API
- cJSON
代碼示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include "cJSON.h"
// MySQL 配置
#define DB_HOST "xxx.xxx.xxx.xxx"
#define DB_USER "root"
#define DB_PASSWORD "xxxxxx"
#define DB_NAME "xxxxx"
void insert_user(MYSQL *conn, int id, const char *name, int age) {
char query[256];
snprintf(query, sizeof(query), "INSERT INTO users (id, name, age) VALUES (%d, '%s', %d)", id, name, age);
if (mysql_query(conn, query)) {
fprintf(stderr, "INSERT failed: %s\n", mysql_error(conn));
}
}
int main() {
// MySQL 連接
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
fprintf(stderr, "mysql_init() failed\n");
return EXIT_FAILURE;
}
if (mysql_real_connect(conn, DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, 0, NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
return EXIT_FAILURE;
}
// 讀取 JSON 文件
FILE *file = fopen("data.json", "r");
if (!file) {
perror("Could not open file");
mysql_close(conn);
return EXIT_FAILURE;
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char *data = malloc(length);
fread(data, 1, length, file);
fclose(file);
// 解析 JSON
cJSON *json = cJSON_Parse(data);
if (json == NULL) {
fprintf(stderr, "cJSON_Parse failed\n");
free(data);
mysql_close(conn);
return EXIT_FAILURE;
}
// 遍歷 JSON 數(shù)組并插入數(shù)據(jù)
cJSON *user;
cJSON_ArrayForEach(user, json) {
int id = cJSON_GetObjectItem(user, "id")->valueint;
const char *name = cJSON_GetObjectItem(user, "name")->valuestring;
int age = cJSON_GetObjectItem(user, "age")->valueint;
insert_user(conn, id, name, age);
}
// 清理
cJSON_Delete(json);
free(data);
mysql_close(conn);
return EXIT_SUCCESS;
}
3. 編譯和運(yùn)行
新建一個(gè)makefile文件:
# 定義變量 CC = gcc CFLAGS = -Wall -g LIBS = -lm -lmysqlclient # 添加 MySQL 客戶端庫 TARGET = json_to_mysql SRC = main.c cJSON.c OBJ = $(SRC:.c=.o) # 默認(rèn)目標(biāo) all: $(TARGET) # 構(gòu)建目標(biāo) $(TARGET): $(OBJ) $(CC) $(CFLAGS) -o $@ $^ $(LIBS) # 編譯 .c 文件為 .o 文件 %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ # 清理目標(biāo) clean: rm -f $(TARGET) $(OBJ) # 偽目標(biāo) .PHONY: all clean
輸入make編譯:

運(yùn)行


到此這篇關(guān)于C語言讀取data.json文件并存入MySQL數(shù)據(jù)庫小案例的文章就介紹到這了,更多相關(guān)C語言讀取data.json文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
結(jié)合C++11新特性來學(xué)習(xí)C++中l(wèi)ambda表達(dá)式的用法
這篇文章主要介紹了C++中l(wèi)ambda表達(dá)式的用法,lambda表達(dá)式的引入可謂是C++11中的一大亮點(diǎn),同時(shí)文中也涉及到了C++14標(biāo)準(zhǔn)中關(guān)于lambda的一些內(nèi)容,需要的朋友可以參考下2016-01-01
C++實(shí)現(xiàn)LeetCode(14.最長共同前綴)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(14.最長共同前綴),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)LeetCode(64.最小路徑和)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(64.最小路徑和),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
OpenCV實(shí)現(xiàn)簡(jiǎn)易標(biāo)定板
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)簡(jiǎn)易標(biāo)定板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

