物联网之sqlite3数据库

https://blog.csdn.net/weixin_39148042/article/details/81164837

数据库

数据库基本概念

常用的数据库

基于嵌入式的数据库

SQLite 基础

创建数据库

数据库常用命令介绍

SQLite编程接口

数据库基本概念

数据(Data):能够输入计算机并能被计算机程序识别和处理的信息集合

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

常用的数据库

大型数据库

    ---Oracle公司是最早开发关系数据库的厂商之一,其产品支持最广泛的操作系统平台。目前Oracle关系数据库产品的市场占有率名列前茅。

    ---IBM 的DB2是第一个具备网上功能的多媒体关系数据库管理系统,支持包Linux在内的一系列平台。

中型数据库

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

小型数据库

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;

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

创建数据库

手工创建:

    ---使用SQLite3工具,通过手工输入SQL命令行完成数据库创建.

    ---用户在Linux的命令行界面中输入SQLite3可启动SQLite3工具

代码创建:

    ---在代码中常动态创建数据库

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

SQLite常用命令介绍

在终端下运行sqlite3   <*.db>,出现如下提示符(表示数据库正常启动):

SQLite  version  3.7.2

Enter “.help” for instructions

Enter SQL statements terminated with a “;”

sqlite>

<*.db> 是要打开的数据库文件。若该文件不存在,则自动创建

SQL命令以.开始:

显示所有命令:sqlite> .help

退出sqlite3:sqlite>.quit              sqlite>.exit

显示当前打开的数据库文件:sqlite>.database

显示数据库中所有表名:sqlite>.tables

查看表的结构:sqlite>.schema  <table_name>

以下为SQL命令,每个命令以;结束

创建新表:sqlite>create  table  <table_name>  (f1  type1, f2  type2,…);

create table stuinfo(id integer, name text, age integer, score float);

向表中添加新记录:sqlite>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);

查询表中所有记录:sqlite>select  *  from  <table_name>;

select * from stuinfo;

按指定条件删除表中记录: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;

删除表:sqlite>drop  table  <table_name>;

drop table stuinfo;

在表中添加字段:sqlite>alter table <table> add column <field> <type> default  …; 

alter table stuinfo add column sex char;

按指定条件查询表中记录:sqlite>select  *  from  <table_name>  where  <expression>;

​select * from stuinfo;   查询表中所有记录
select * from stuinfo where score = 80; 查询表中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中不允许删除字段,可以通过下面步骤达到同样的效果

    ---sqlite> create table stu as select no, name, score from student //复制表student中部分字段的内容,创建一个新表stu

    ---sqlite> drop table student   //删除表student

    ---sqlite> alter table stu rename to student  //将表stu更名为student

数据库设置主键:create table info(id integer primary key autoincrement, name vchar);

SQLite编程接口

int   sqlite3_open(char  *path,   sqlite3 **db);

    ---功能:打开sqlite数据库

    ---path:数据库名称(包括数据库文件路径 )

    ---db:指向sqlite句柄的指针

    ---返回值:成功返回0(SQLITE_OK),失败返回错误码(非零值)

int   sqlite3_close(sqlite3 *db);

    ---功能:关闭sqlite数据库        

    ---返回值:成功返回0(SQLITE_OK),失败返回错误码

const  char  *sqlite3_errmg(sqlite3 *db);        

    ---返回值:得到错误信息的描述

int sqlite3_exec(
   sqlite3* db,                                  /* An open database */
  const char *sql,                           /* SQL to be evaluated */
  int (*callback)(void* arg,int,char**,char**),  /* Callback function */
  void * arg,                                    /* 1st argument to callback */
  char **errmsg                              /* Error msg written here */
  );

    ---功能:执行一条sql语句

    ---db:数据库句柄

    ---sql:SQL语句

    ---callback:回调函数,只有在查询时,才传参

    ---errmsg:错误信息指针的地址

    ---返回值:成功返回0(SQLITE_OK),失败返回错误码

int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name); //查询回调函数

    ---功能:查询语句执行之后,会回调此函数

    ---para:接收sqlite3_exec 传递来的参数

    ---f_num:记录中包含的字段数目

    ---f_value:包含每个字段值的指针数组

    ---f_name:包含每个字段名称的指针数组

    ---返回值:成功返回0,失败返回-1

不使用回调函数执行SQL语句:

int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);

    ---功能:执行SQL操作

    ---db:数据库句柄

    ---sql:SQL语句

    ---resultp:用来指向sql执行结果的指针

    ---nrow:满足条件的记录的数目

    ---ncolumn:每条记录包含的字段数目

    ---errmsg:错误信息指针的地址

    ---返回值:成功返回0,失败返回错误码

void sqlite3_free_table(char **result);

    sqlite3 * db;
	char buf[128] = {};
	if((sqlite3_open(SQL_OPEN, &db)) != SQLITE_OK)  //打开数据库(不存在就新建)
	{
		printf("%s\n", sqlite3_errmsg(db));
		exit(1);
	}

	sprintf(buf, "create table register(name char PRIMARY KEY, password char);"); //sql语句:新建一个数据库表(表中包含name 和 password 两个字段,其中name 设置为主键<不允许记录中有相同的name>)
	if((sqlite3_exec(db, buf, NULL, NULL, &errmsg) != SQLITE_OK))
	{
		if(strcmp(errmsg, "table register already exists"))
		{
			printf("%s\n", errmsg);
			exit(1);
		}
	}

	sprintf(buf, "create table history(name char, word char, date char);");
	if((sqlite3_exec(db, buf, NULL, NULL, &errmsg) != SQLITE_OK))
	{
		if(strcmp(errmsg, "table history already exists"))
		{
			printf("%s\n", errmsg);
			sqlite3_close(db);
			exit(1);
		}
	}

猜你喜欢

转载自blog.csdn.net/weixin_39148042/article/details/81163349