C语言,用cJSON将从mysql数据库中查询的数据转为json格式

流淌的青春 2020-07-13 04:47:23
下面是能遍历出表中数据的结果,之后怎么封装到cJSON中呢,cJSON应该怎么写,这里不会了,求大神指导

int main()
{
MYSQL *conn = NULL;
MYSQL_RES *res = NULL;
MYSQL_ROW row = NULL;

conn = mysql_init(NULL);
if(mysql_real_connect(conn,host,user,password,db_name,0,NULL,0) == NULL)
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}

char sql[] = "select g.group_id, g.parent_id, g.ancestors, g.group_name, g.order_num, g.status\
from sys_group g\
where g.parent_id = (SELECT group_id from sys_group_handle where handle_num = 1001)\
and g.del_flag = '0'\
order by order_num asc";
if(mysql_query(conn,sql))
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
while((row = mysql_fetch_row(res)) != NULL)
printf("%s\t%s\t%s\t%s\t%s\t\n",row[0],row[1],row[2],row[3],row[4]);


mysql_free_result(res);
mysql_close(conn);
return 0;
}
...全文
227 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
流淌的青春 2020-07-13
  • 打赏
  • 举报
回复
3 2 0,1.2 啦啦 1 追加 输出的格式类似这个样子 ,还没有加字段名字,只是遍历了一下数据

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧