600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 树莓派安装使用数据库SQLite

树莓派安装使用数据库SQLite

时间:2023-07-19 11:18:44

相关推荐

树莓派安装使用数据库SQLite

陈拓/07/29-/07/29

1. 概述

SQLite官网:/index.html

最新版本:

官方对SQLite的介绍:

SQLite是一个C语言库,它实现了一个小型、快速、自包含、高可靠性、全功能的SQL数据库引擎。SQLite是世界上使用最多的数据库引擎。SQLite内置于几乎所有的移动电话和大多数计算机中,并捆绑在人们每天使用的无数其他应用程序中。

SQLite文件格式是稳定的、跨平台的、向后兼容的,开发人员保证至少在2050年以前保持这种格式。SQLite数据库文件通常用作在系统之间传输丰富内容的容器,并作为数据的长期存档格式。目前有超过1万亿(1 trillion)个SQLite数据库在使用中。

SQLite源代码在公共域中,每个人都可以自由使用。

2. 安装 SQLite

树莓派换源

见《树莓派安装Web服务器Boa和CGIC》/chentuo2000/article/details/108535232

安装sqlite3

sudo apt-get update

sudo apt-get install sqlite3

版本检测:sqlite3 -version

2. 数据库和表的创建

SQLite是以文件形式存在的,一个数据库就是一个文件。

先重建一个工作目录

mkdir idata

cd idata

创建数据库文件smarthome.db

sqlite3 smarthome.db

在当前目录下创建一个数据库文件smarthome.db,同时挂载到SQLite3上。如果smarthome.db已存在就打开并挂载这个文件。

查挂载到数据库上的数据库文件

.databases

smarthome.db是主数据库main。

创建表temperature

在我们的智能家居系统中有一些温度传感器,我们将这些温度传感器的数据存在这张表中。

create table temperature(id INTEGER PRIMARY KEY,deviceid varchar(20) default '0',time varchar(20) default '0',battery_voltage int default 0,celsius_temp real default 0);

查所有的表

.tables

查表temperature的结构

.schema temperature

3. 基本操作

插入数据

insert into temperature (deviceid, time, battery_voltage, celsius_temp) values('temp001', '-07-28 17:25:35', 3451, 26.75);insert into temperature (deviceid, time, battery_voltage, celsius_temp) values('temp001', '-07-28 17:25:45', 3454, 25.50);insert into temperature (deviceid, time, battery_voltage, celsius_temp) values('temp001', '-07-28 17:25:55', 3453, 24.35);

查询数据

查询所有记录:

select * from temperature;

查询第二条记录:

select * from temperature where id=2;

查看前2条记录:

select * from temperature limit 2;

查看第2-3条记录:

select * from temperature limit 1,3;

修改数据

修改第二条记录,将温度从25.5改成25.65:

update temperature set celsius_temp=25.65 where id=2;

删除数据

删除第二条记录:

delete from temperature where id=2;

删除所有记录:

delete from temperature;

删除表

drop table temperature;

.tables

退出SQLite

.quit

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。