sqlite3 and json


========================================================
一、数据库sqlite3
二、json
========================================================
一、sqlite3
   作用:1、掉电保存
          2、增删查改更方便

    为什么用sqlite3?
        1、小巧
        2、开源免费
    怎么用?
        1、在线安装
        sudo apt install sqlite3
        
        2、离线安装
        获取sqlite3源码
        编译:
            1、./configure --prefix=$(pwd)/_install        //生成Makefile   prefix:指定安装路径
            2、make                                        //生成bin、include、lib库  *.so
            3、make install
            4、部署                    
                //把bin、include、lib目录下的内容拷贝到系统目录
                //bin文件的系统目录:/bin
                //include文件的系统目录:  /usr/include
                //lib文件的系统目录:    /lib      /usr/lib
            
        3、使用
            .help
            .quit
            .exit
        命令行
            数据存放在    date.db
            创建数据库文件:touch date.db
            打开:            sqlite3 date.db
            
            .databases        //查看数据库文件
            
            CREATE TABLE class22071(num int,name char[20],age int);        //创建表
            CREATE TABLE class22075 (num int default 0,name char[20] default "NULL");  //设置默认值
            
            insert into class22071 values (1,"aa",18);                    //插入数据
            insert into class22071 values (2,"bb",19),(3,"xx",20),(4,"yy",21);        //插入多条数据
            insert into class22074 (num) values (1);                    //插入某一个字段的值
            
            delete from class22071 where num = 1;                        //删除
                                        num = 1 and name = "cc"        
                                    or    num > 0  and num < 10
                                        
            select * from class22071;                        //查询表格全部内容
            select * from class22071 where name = "bb";        //按where条件查找
            select age from class22071 where name = "bb";    //按where条件查找指定的列
        
            update class22071 set num = 100 where name = "bb";        //改数据
            
    代码:
        https://www.sqlite.org/c3ref/funclist.html        //sqlite3 函数网址
        
        printf("insert into ..... %s ,%d, %s",name,num,...);
        sprintf(buff,"insert into ..... %s ,%d, %s",name,num,...);
        
        int sqlite3_exec(
            sqlite3*,                                  /* An open database */  打开的数据库
            const char *sql,                           /* SQL to be evaluated */  要执行的sql语句
            int (*callback)(void*,int,char**,char**),  /* Callback function */  执行sql语句时对应的回调函数
            void *,                                    /* 1st argument to callback */  回调函数的参数
            char **errmsg                              /* Error msg written here */  存放错误信息
        );
        
        
二、json库
    作用:数据交换
    操作:
        拷贝:cp /mnt/hgfs/ubuntu_share/json-c-0.9.tar.gz  ./
        解压:tar -xvf json-c-0.9.tar.gz
        进入:cd json-c-0.9/
        ./configure --prefix=$(pwd)/_install            //生成Makefile,指定安装目录
        make                                            //编译
        make install                                    //安装
        部署    

    json格式:把数据与键值一一对应,数据传输双方约定好同一键值,
            使用接口API根据键值操作jason对象(jason_object)存储或取得数据。
    一般使用:
        数据-》(封装)jason对象-》String格式-》。。。传输。。。-》String格式-》(解析)jason对象-》取得数据
        (int、char..)数据,与键值成对存入json对象—————————————————————————————————>通过键值从json对象取得数据

    json接口API(在json中所有数据类型(arry、int、string、char)都是一个jason对象)

    1、数据的封装(单对象(int、char、string)和数组(arry))
    (1)新建对象:
        A.创建一个Json对象:
            struct json_object * json_object_new_object (void)
        B.创建一个Json数组对象:
            struct json_object * json_object_new_array (void)
        C.销毁json对象
            void json_object_put (struct json_object *obj)

    (2)json对象的转换(普通类型->json对象):
        1:struct json_object * json_object_new_int (int i)
        2:struct json_object * json_object_new_double (double d)
        3:struct json_object * json_object_new_string (const char *s)
        4:struct json_object * json_object_new_boolean (boolean b)
        5:struct json_object * json_object_new_string_len (const char *s, int len)

    (3)json对象的处理
        A.普通对象
            添加:void json_object_object_add (struct json_object *obj, const char *key, struct json_object *val)
            删除:void json_object_object_del (struct json_object *obj, const char *key)
            查询:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
            根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
        B.数组对象
            获取长度:int json_object_array_length (struct json_object *obj)
            添加:int json_object_array_add (struct json_object *obj, struct json_object *val)
            指定位置添加:int json_object_array_put_idx (struct json_object *obj, int idx, struct json_object *val)
            获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)

    (4)json_object To 字符流
            const char * json_object_to_json_string (struct json_object *obj)

    2、数据的解析(解析获取到的json格式字符流)

    (1)字符流 To json_object
            struct json_object*    json_tokener_parse(const char *str)

    (2)对象获取
        A.普通对象
            根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
        B.数组对象
            获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)
    
    (3)对象的转换(数据还原)
            bool型:boolean json_object_get_boolean (struct json_object *obj)
            double型:double json_object_get_double (struct json_object *obj)
            整型:int json_object_get_int (struct json_object *obj)
            字符数组:const char * json_object_get_string (struct json_object *obj)
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅醉_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值