当前位置: 首页 > news >正文

【嵌入式数据库之sqlite3】

目录

一.数据库基本概念(理解)

1.数据

2.数据库

二.常用的数据的数据库(了解)

1.大型数据库

2.中型数据库

3.小型数据库

三.基于嵌入式的数据库(了解)

四.SQLite基础(了解)

五.创建数据库(熟练)

1.手工创建

2.代码创建

六.SQLite编程接口

七.代码示例(学生管理系统)

八.水果店铺管理系统


一.数据库基本概念(理解

1.数据

        能够输入计算机并能够计算机程序识别和处理的信息集合。(不只是数字)

2.数据库

        数据库是在数据库管理系统管理和控制之下,存放在存储介质上的数据集合。

二.常用的数据的数据库(了解)

1.大型数据库

        Oracle公司是最早开发关系数据库的厂商之一,其产品支持最广泛的操作系统平台。目前Oracle关系数据库产品的市场占有率名列前茅。 IBM 的DB2是第一个具备网上功能的多媒体关系数据库管理系统,支持包Linux在内的一系列平台。

2.中型数据库

        Server是微软开发的数据库产品,主要支持windows平台。

3.小型数据库

        mySQL是一个小型关系型数据库管理系统,开发者为瑞典MySQL AB公司,2008年被Sun公司收购,开放源码。

        

三.基于嵌入式的数据库(了解)

        基于嵌入式Linux的数据库主要有SQLite, Firebird, Berkeley DB, eXtremeDB

        Firebird是关系型数据库,功能强大,支持存储过程、SQL兼容等

        SQLite关系型数据库,体积小,支持ACID事务

        Berkeley DB中并没有数据库服务器的概念,它的程序库直接链接到应用程序中

        eXtremeDB是内存数据库,运行效率高

四.SQLite基础(了解)

        SQLite的源代码是C,其源代码完全开放。SQLite第一个Alpha版本诞生于2000年5月。 他是一个轻量级的嵌入式数据库。

SQLite有以下特性:

         零配置一无需安装和管理配置;

        储存在单一磁盘文件中的一个完整的数据库;

        数据库文件可以在不同字节顺序的机器间自由共享;

        支持数据库大小至2TB;

        足够小,全部源码大致3万行c代码,250KB;

        比目前流行的大多数数据库对数据的操作要快;

五.创建数据库(熟练)

1.手工创建

        使用SQLite3工具,通过手工输入SQL命令行完成数据库创建. 用户在Linux的命令行界面中输入SQLite3可启动SQLite3工具

       数据库的安装

 sudo dpkg -i  *.deb  //本地安装包安装sudo apt-get install sqlite3  //在线安装

        数据库常用命令

        (1) 系统命令都以“ . ”开头

.exit //退出
.quit //退出
.table //查看表
.schema <table_name> //查看表的结构 
.help //显示所有命令
.database //显示当前打开的数据库文件

打开数据库文件 

在终端下运行sqlite3   <*.db>,出现如下提示符
SQLite  version  3.7.2
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite>
<*.db> 是要打开的数据库文件。若该文件不存在,则自动创建

         (2)sql执行语句,都以“ ;”结尾

创建一张表

create  table  <table_name>  (f1  type1, f2  type2,…);crerate table stuinfo(id integer ,name char ,age intger,score float);

插入一条记录 

insert  into  <table_name>  values (value1, value2,…);insert into stuinfo values(1001, 'zhangsan', 18, 80);
insert into stuinfo (id, name, score) values(1002, 'lisi', 90);

 查看数据库记录(where后面是条件)

select  *  from  <table_name>  where  <expression>;select * from stuinfo;
select * from stuinfo where score = 80;
select * from stuinfo where score = 80 and name= 'zhangsan';
select * from stuinfo where score = 80 or name='wangwu';
select name,score from stuinfo;  查询指定的字段
select * from stuinfo where score >= 85 and score < 90;

 删除一条记录

sqlite>delete  from  <table_name>  where  <expression>;delete from stuinfo where id=1003 and name='zhangsan';

更新一条记录 

sqlite>update  <table_name>  set  <f1=value1>, <f2=value2>… where<expression>;  update stuinfo set age=20 where id=1003;
update stuinfo set age=30, score = 82 where id=1003;

 删除一张表

drop  table  <table_name>drop table stuinfo;

 增加一列

alter table <table> add column <field> <type> default  …; alter table stuinfo add column sex char;

 删除一列(通过创建新的表来删除)

create table stu as select id, name, score from stuinfo;
drop table stuinfo;
alter table stu rename to stuinfo;

2.代码创建

        在程序运行过程中,当需要进行数据库操作时,应用程序会首先尝试打开数据库,此时如果数据库并不存在,程序则会自动建立数据库,然后再打开数据库

六.SQLite编程接口

        1.打开(创建)sqlite数据库

int   sqlite3_open(char  *path,   sqlite3 **db);path:数据库文件路径(数据库名称)
db:指向sqlite句柄的指针
返回值:成功返回0,失败返回错误码(非零值)

        2.关闭sqlite数据库       

int   sqlite3_close(sqlite3 *db);返回值:成功返回0,失败返回错误码

        3.得到错误信息的描述

const  char  *sqlite3_errmg(sqlite3 *db);         
返回值:返回错误信息

        4.执行SQL语句操作 

Int sqlite3_exec(
sqlite3 *db,
const  char  *sql, 
sqlite3_callback callback, 
void *, char **errmsg
);db:数据库句柄
sql:SQL语句
callback:回调函数
errmsg:错误信息指针的地址
返回值:成功返回0,失败返回错误码/* Callback function */
功能:查询语句执行之后,会回调此函数
查询回调函数:int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name);参数:arg   接收sqlite3_exec 传递来的参数ncolumns 列数(记录中包含的字段数目)f_value 列的值的地址(包含每个字段值的指针数组)f_name   列的名称(包含每个字段名称的指针数组)
返回值:成功返回0,失败返回-1

        5. 不使用回调函数执行SQL操作查询

不使用回调函数执行SQL语句
int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);db:数据库句柄
sql:SQL语句
resultp:用来指向sql执行结果的指针
nrow:满足条件的记录的数目
ncolumn:每条记录包含的字段数目
errmsg:错误信息指针的地址
返回值:成功返回0,失败返回错误码

七.代码示例(学生管理系统)

        管理学生的信息

八.水果店铺管理系统

        管理水果种类,重量,价格等信息和信息变动记录

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <time.h>#define  DATABASE  "fruit.db"
#define TABLES "fruit"
#define  N  128int do_insert(sqlite3 *db,char *buf);
int do_update_weight(sqlite3 *db,char *buf);
int do_update_price(sqlite3 *db);
int do_query(sqlite3 *db);
int callback(void *arg, int f_num, char ** f_value, char ** f_name);int main(int argc, const char *argv[])
{sqlite3 *db;char *errmsg;int n;char sql[128] = {};char buf[128] = {};time_t t;struct tm *tp;if(sqlite3_open(DATABASE, &db) != SQLITE_OK){printf("%s\n", sqlite3_errmsg(db));return -1;}else{printf("open DATABASE success.\n");}sprintf(sql,"create table if not exists '%s' (id integer primary key autoincrement,name char,weight float,price float,time char);",TABLES);if(sqlite3_exec(db, sql,NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Create or open table success.\n");}time(&t);tp = localtime(&t);sprintf(buf,"%d-%02d-%02d %02d:%02d:%02d\n",tp->tm_year+1900, tp->tm_mon+1,tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);printf("%s",buf);while(1){puts("-----------------------------------------");do_query(db);puts("-----------------------------------------");puts("");printf("********************************************\n");printf("1: insert 插入  2:query 查询 3:trade 交易 4:update 更新 5:quit 退出系统\n");printf("********************************************\n");printf("Please select:");scanf("%d", &n);switch(n){case 1:do_insert(db,buf);break;case 2:do_query(db);break;case 3:do_update_weight(db,buf);break;case 4:do_update_price(db);break;case 5:printf("main exit.\n");sqlite3_close(db);exit(0);break;default :printf("Invalid data n.\n");}}return 0;
}int do_insert(sqlite3 *db,char *buf)
{char name[32] = {};float weight;float price;char sql[N] = {};char *errmsg;printf("Input fruit name:");scanf("%s", name);getchar();printf("Input weight:");scanf("%f", &weight);printf("Input price:");scanf("%f", &price);sprintf(sql, "insert into '%s' (name,weight,price,time)values('%s', '%.3f', '%.3f','%s')",TABLES, name, weight, price,buf);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Insert done.\n");}return 0;
}int do_update_weight(sqlite3 *db,char *buf)
{int id;char sql[N] = {};char *errmsg;float weight;printf("Input id:");scanf("%d", &id);printf("Input weight:");scanf("%f", &weight);sprintf(sql, "update '%s' set weight='%f',time='%s' where id=%d",TABLES, weight,buf,id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Delete done.\n");}return 0;
}int do_update_price(sqlite3 *db)
{float price;int id;char sql[N] = {};char *errmsg;printf("Input id:");scanf("%d", &id);printf("Input alter price:");scanf("%f", &price);sprintf(sql, "update '%s' set price='%f' where id=%d",TABLES , price,id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("update done.\n");}return 0;
}int callback(void *arg, int f_num, char ** f_value, char ** f_name)
{int i = 0;for(i = 0; i < f_num; i++){//	printf("%-8s %s", f_value[i], f_name[i]);printf("%-8s", f_value[i]);}putchar(10);puts("-----------------------------------------");return 0;
}int do_query(sqlite3 *db)
{char *errmsg;char sql[N] = {};sprintf(sql,"select * from '%s';",TABLES);if(sqlite3_exec(db, sql, callback,NULL , &errmsg) != SQLITE_OK){printf("%s", errmsg);}else{printf("select done.\n");}return 0;
}

http://www.lryc.cn/news/161571.html

相关文章:

  • Android 9.0 pms中关于启动app时获取app的ActivityInfo信息相关源码分析
  • 华为数通方向HCIP-DataCom H12-821题库(单选题:321-340)
  • 《TCP/IP网络编程》阅读笔记--基于TCP的服务器端/客户端
  • 【每日一题】43. 字符串相乘
  • 机器学习——K最近邻算法(KNN)
  • 同步FIFO的verilog实现(1)——计数法
  • python正则表达式笔记1
  • YOLO目标检测——口罩规范佩戴数据集+已标注xml和txt格式标签下载分享
  • Android 13 - Media框架(9)- NuPlayer::Decoder
  • 23.09.5 《CLR via C#》 笔记5
  • laravel部署api项目遇到问题总结
  • lintcode 1646 · 合法组合【字符串DFS, vip 中等 好题】
  • 【多线程】线程安全 问题
  • 【用unity实现100个游戏之11】复刻经典消消乐游戏
  • 若依cloud 修改包名等
  • 健康系统练习
  • 网络协议从入门到底层原理学习(一)—— 简介及基本概念
  • centos密码过期导致navicat无法通过SSH登录阿里云RDS问题
  • 对于pytorch和对应pytorch网站的探索
  • 和AI聊天:动态规划
  • 微信小程序——使用插槽slot快捷开发
  • 大数据技术之Hadoop:使用命令操作HDFS(四)
  • 静态路由配置实验:构建多路由器网络拓扑实现不同业务网段互通
  • Python函数的概念以及定义方式
  • 【数学建模竞赛】超详细Matlab二维三维图形绘制
  • 2023国赛数学建模E题思路代码 黄河水沙监测数据分析
  • 窗口延时、侧输出流数据处理
  • 发送HTTP请求
  • 高等工程数学张韵华版第四章课后题答案
  • wpf C# 用USB虚拟串口最高速下载大文件 每包400万字节 平均0.7s/M,支持批量多设备同时下载。自动识别串口。源码示例可自由定制。