数据库
现在要用的目录中创建数据库 touch date.db
打开数据库 sqlite3 date.db
打开之后进入sqlite3界面
查看数据库文件 .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); //插入某一个字段的值
删
drop table <table_name>; //删除表格
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"; //改数据
C语言中使用数据库函数:(官方函数)
https://www.sqlite.org/c3ref/funclist.html
函数
作用:打开一个数据库文件
sqlite3_open_v2(
const char *filename, /*需要打开的数据库文件的路径和名字*/
sqlite3 **ppDb, /*这个数据库的句柄 */
int flags, /* 权限 */
const char *zVfs /* 要使用的 VFS 模块的名称 */
);
作用:对数据库进行各种操作,比如增删改查
sqlite3_exec(
sqlite3*, /* 权柄,用来区分数据库库 */
const char *sql, /* 需要执行的sql语句 */
int (*callback)(void*,int,char**,char**), /* (回调函数) 只有执行查询sql语句的时候才需要 */
void *, /* 回调的第一个参数 */
char **errmsg /* 入错误消息 */
);
注:回调函数需要自己写,需要做什么就写什么
例子:
/*
*第一个参数:传参用的
*第二个参数:列的个数、字段的个数
*第三个参数:查询到的数据,data[0],data[1]....
*第四个参数:字段的名称
* */
/*特别注意!!!
*查询到多少条数据,就会调用多少次回调函数
*一定要return
*/
int callback1(void* arg ,int num ,char** data,char** name)
{
*(int *)arg = 1;
for(int i = 0; i < num;i++ )
printf("%s ==> %s ",name[i],data[i]);
printf("\r\n");
return 0;
}
作用:对数据库进行各种操作
int sqlite3_get_table(
sqlite3 *db, /* 一个打开的数据库 */
const char *zSql, /* 要评估的 SQL */
char ***pazResult, /* 查询结果 (放的是查询到的数据)*/
int *pnRow, /* 此处写入的结果行数 (查到了几条数据)*/
int *pnColumn, /* 此处写入的结果列数 (数据有多少列)*/
char **pzErrmsg /* 此处写入的错误信息 */
);
作用:关闭数据库
sqlite3_close(ppDb);
各函数使用例子:
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
sqlite3 * ppDb ; //句柄
int callback1(void* arg ,int num ,char** data,char** name);
int main()
{
int error;
error = sqlite3_open_v2("./date.db", &ppDb,SQLITE_OPEN_READWRITE,NULL);
if(error != SQLITE_OK)
{
printf("%s\r\n",sqlite3_errmsg(ppDb));
return -1;
}else
printf("open sqlite3 success\r\n");
/*********插入**************************/
int num,age;
char name[20];
char buff[100];
memset(buff,0,sizeof(buff));
for(int i = 0; i < 0;i++){
printf("请输入学号、姓名、年龄:\r\n");
scanf("%d%s%d",&num,name,&age);
sprintf(buff,"insert into class22071 values (%d,\"%s\",%d);",num,name,age);
printf("%s\r\n",buff);
error = sqlite3_exec(
ppDb, /* An open database */ /*权柄,用来区分数据库*/
buff, /* SQL to be evaluated */ /*需要执行的sql语句*/
NULL, /* Callback function */ /*只有执行查询sql语句的时候才需要*/
NULL, /* 1st argument to callback */ /*同第三个参数*/
NULL /* Error msg written here *//*错误信息*/
);
if(error != SQLITE_OK)
{
printf("%s\r\n",sqlite3_errmsg(ppDb));
return -1;
}
}
memset(buff,0,sizeof(buff));
strcpy(buff,"delete from class22071 where name = \"bb\";");
error = sqlite3_exec(ppDb,buff,NULL,NULL,NULL);
if(error != SQLITE_OK)
{
printf("%s\r\n",sqlite3_errmsg(ppDb));
return -1;
}
#if 0
/***************查询*************************/
memset(buff,0,sizeof(buff));
strcpy(buff,"select * from class22071;");
int flag = 0;
error = sqlite3_exec(ppDb,buff,callback1,(void *)&(flag),NULL);
if(error != SQLITE_OK)
{
printf("%s\r\n",sqlite3_errmsg(ppDb));
return -1;
}
if(flag != 0)
printf("查询到数据\r\n");
else
printf("没有查询到数据\r\n");
#endif
#if 1
/**************查询(没有回调函数)*******************************/
char ** pazResult;
int pnRow,pnColumn;
memset(buff,0,sizeof(buff));
strcpy(buff,"select * from class22071;");
sqlite3_get_table(
ppDb, /* An open database */
buff, /* SQL to be evaluated */
&pazResult, /* Results of the query */ /*放的是查询到的数据*/
&pnRow, /* Number of result rows written here */ /*查到了几条数据*/
&pnColumn, /* Number of result columns written here */ /*数据有多少列*/
NULL /* Error msg written here */
);
if(pnRow != 0){
int k = pnColumn;
printf("查询到了%d条数据\r\n",pnRow);
printf("列:%d\r\n",pnColumn);
for(int i = 0; i < pnRow ;i++)
{
for(int j = 0 ; j < pnColumn;j++)
printf("%s==>%s ",pazResult[j],pazResult[k++]);
printf("\r\n");
}
}
#endif
sqlite3_close(ppDb);
return 0;
}
int callback1(void* arg ,int num ,char** data,char** name)
{
*(int *)arg = 1;
for(int i = 0; i < num;i++ )
printf("%s ==> %s ",name[i],data[i]);
printf("\r\n");
return 0;
}
json 库
数据->(封装)jason对象->String格式->......传输......->String格式->(解析)jason对象->取得数据
打包
数据的封装(单对象(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)
解包
数据的解析(解析获取到的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)
例子:
#include <stdio.h>
#include <json/json.h>
#include <string.h>
struct data {
char name[20];
char acc[10];
char passwd[10];
int age;
double sanwei;
};
void dabao(struct data mydata,char * buff);
void jiebao(char * buff,struct data * mydata);
int main()
{
char buff[300];
struct data mydata = {
.name = "hbb",
.acc = "12345",
.passwd = "54321",
.age = 18,
.sanwei = 30.98,
};
struct data youdata;
dabao(mydata,buff);
printf("buff ==> %s\r\n",buff);
jiebao(buff,&youdata);
printf("===>%s\r\n",youdata.name);
printf("===>%s\r\n",youdata.acc);
printf("===>%s\r\n",youdata.passwd);
printf("===>%d\r\n",youdata.age);
printf("===>%lf\r\n",youdata.sanwei);
return 0;
}
void dabao(struct data mydata,char * buff)
{
// char buff1[300];
// memset(buff1,0,sizeof(buff1));
struct json_object * obj1 = NULL;
//struct json_object * obj2 = NULL;
obj1 = json_object_new_object() ;
//obj2 = json_object_new_string (mydata.name);
//json_object_object_add(obj1,"name",obj2);
json_object_object_add (obj1, "name", json_object_new_string(mydata.name));
json_object_object_add (obj1, "acc", json_object_new_string(mydata.acc));
json_object_object_add (obj1, "passwd", json_object_new_string(mydata.passwd));
json_object_object_add (obj1, "age", json_object_new_int(mydata.age));
json_object_object_add (obj1, "sanwei", json_object_new_double(mydata.sanwei));
//strcpy(buff1,json_object_to_json_string(obj1));
// printf("%s\r\n",buff1);
strcpy(buff,json_object_to_json_string(obj1));
}
void jiebao(char * buff,struct data * mydata)
{
struct json_object * obj1 = NULL;
// struct json_object * obj2 = NULL;
obj1 = json_tokener_parse(buff);
// obj2 = json_object_object_get (obj1, "name");
// printf("--1---==>%s\r\n",json_object_get_string (obj2));
strcpy(mydata->name,json_object_get_string(json_object_object_get(obj1,"name")));
strcpy(mydata->acc,json_object_get_string(json_object_object_get(obj1,"acc")));
strcpy(mydata->passwd,json_object_get_string(json_object_object_get(obj1,"passwd")));
mydata->age = json_object_get_int(json_object_object_get(obj1,"age"));
mydata->sanwei = json_object_get_double(json_object_object_get(obj1,"sanwei"));
}