安装和卸载sqlite3 + 使用C语言API将json文件写入数据库

from:https://blog.csdn.net/jinking01/article/details/100152038
from:https://blog.csdn.net/hkyshl/article/details/88046537

一、在Linux环境下安装SQLite

手动安装:

1、去官网下载安装包

2、解压文件 tar -zxvf sqlite-autoconf-3220000.tar.gz 后进入解压后的目录;

3、./configure ;

4、make ;

5、sudo make install

自动安装:
sudo apt install sqlite3

二、卸载SQLite

第一种、linux命令行卸载

1)浏览已安装的程序。要查看已安装的软件包列表,请输入以下命令。请注意你希望卸载的软件包的名称。这样一会儿可以查看是否真的卸载。

dpkg --list

2)卸载程序和所有配置文件。在终端中输入以下命令:

sudo apt-get --purge remove sqlite3

只卸载程序。如果你移除程序但保留配置文件,请输入以下命令:

sudo apt-get remove sqlite3

3)安装

sudo apt-get install sqlite3

第二种、进入sqlite文件夹卸载
sudo apt-get --purge remove sqlite3,但是没有成功,大概是因为不是通过乌版图apt-get安装的原因吧。

进入解压后的sqlite目录下和安装一样;
./configure再执行make(make失败就删除目录中的sqlite3文件);
最后执行make uninstall.结果显示卸载成功.

三、ubuntu上验证是否SQLite3是否启用了json1扩展

from:https://blog.csdn.net/jinking01/article/details/100152038

执行sqlite3:select json_type(’[“a”, “b”, 1]’);

输出结果若为array,则加载JSON插件成功

四、编写代码将JSON文件写入数据库中

/*json_test.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

#include "cJSON.h"


#define JSONFILENAME     "wares_short.Json"
#define DATABASEFILENAME "lancent.db"
#define DBG_PRINTF(...) 

int main(int argc, char * argv [ ])
{
	FILE* fp = fopen(JSONFILENAME, "rb"); //打开JSON文件
	if(fp == NULL)
	{
		printf("Can't open json file\n");
		return 0;
		
	}
	fseek(fp, 0x0, SEEK_END); 						//文件指针位置调整到文件尾
	int size = ftell(fp) + 1; 						//计算出文件一共有多少字符
	char* buf = (char*)malloc(sizeof(char) * size); //分配缓存
	memset(buf, 0x0, sizeof(char)*size);//清空缓存

	rewind(fp);							//将文件指针调整到文件头
	fread(buf, sizeof(char), size-1, fp);//将JSON文件写入缓存

	cJSON* obj = cJSON_Parse(buf); 		//解析对象
	cJSON* node = NULL;
	if(cJSON_HasObjectItem(obj, "goodsinfo") == 0)//判断是否有goodsinfo
	{
		printf("not fond 'shopid'\n");
		goto js_done;
	}

	node = cJSON_GetObjectItem(obj, "goodsinfo"); //根据键goodsinfo找到结点
	if(node->type != cJSON_Array)					//判断goodsinfo的类型是不是数组[{},{},{}]
	{
		printf("shopid's type is not Array\n");
		goto js_done;
	}

	int i = 0;
	cJSON* dnode = NULL;

	int lenth = cJSON_GetArraySize(node);   //得到goodsinfo数组的个数
	for(i=0; i<lenth; i++)
	{
		dnode = cJSON_GetArrayItem(node, i);  //根据i下标得到结点
		if(dnode->type != cJSON_Object)       //判断结点的类型是不是对象
		{
			printf("shopid's type is not Object\n");
			continue;
		}
		printf("---------\nsku:%d\n", cJSON_GetObjectItem(dnode, "sku")->valueint);
		printf("name:%s\n", cJSON_GetObjectItem(dnode, "name")->valuestring);
	}

	sqlite3* db = NULL;
	char* errmsg = NULL;
	char sql_insert[128] = {0};
	int index = 0;
	cJSON* info;
	int sku = 0;  
	char* name = NULL;
	int price1 = 0;

	if(sqlite3_open(DATABASEFILENAME, &db) != SQLITE_OK) //打开数据库
	{
		printf("errmsg:%s\n", sqlite3_errmsg(db));
		goto db_done;
	}

	for(index; index<lenth; index++)
	{
		info = cJSON_GetArrayItem(node, index);  //根据下标得到数组
		if(info->type != cJSON_Object)
		{
			printf("shopid's type is not Object\n");
			continue;
		}

		sku    = cJSON_GetObjectItem(info, "sku")->valueint; //得到sku的值
		name   = cJSON_GetObjectItem(info, "name")->valuestring;//得到name的值
		price1 = cJSON_GetObjectItem(info, "price1")->valueint;//得到price1的值
		sprintf(sql_insert, "insert into tb_goods(sku, name, price1) values(%d, '%s', %d)", sku, name, price1);//将数据插入表的命令赋值给sql_insert
		if(sqlite3_exec(db, sql_insert, NULL, NULL, &errmsg) != SQLITE_OK)  //执行命令
		{
			printf("%s\n", errmsg);
			goto db_done;
		}
		else
		{
			printf("Insert OK !\n");
		}
	}

	

	js_done:
		fclose(fp);   //关闭json文件
		free(buf);		//释放内存
		cJSON_Delete(obj); //释放cJSON结构体对应的内存空间,因为是以JSON结构体形成双向链表存入内存中的

	db_done:		
		sqlite3_close(db);  //关闭数据库
		if(errmsg != NULL)   //如果有错误信息,则释放
		{
			sqlite3_free(errmsg);
		}
			
	return 0;
}

编译: gcc json_test -o json_test.c -lm -lsqlite3

发布了56 篇原创文章 · 获赞 3 · 访问量 2375

猜你喜欢

转载自blog.csdn.net/qq_40674996/article/details/102457293