您的位置:首页 > 数据库

树莓派学习笔记——SQLite操作简述

2014-08-03 09:25 302 查看
0 前言 本文介绍如何在树莓派中利用SQLite数据库保存CPU温度数据。SQLite是一款轻量级零配置数据库,非常适合在树莓派和其他嵌入式系统中使用。SQLite文档详细资料丰富,本文不会详细解释SQLite数据库操作的方方面面,只能结合具体场景按需说明。本文介绍的SQLite技巧也可以在其他平台使用,并不局限于树莓派。 本文继续折腾树莓派温度,希望从中可以玩出新花样。
【相关博文】 【树莓派学习笔记——索引博文】——更多博文请关注。 【树莓派学习笔记——获取树莓派CPU温度】 【树莓派学习笔记——定时向yeelink上传树莓派CPU温度
1 创建表和插入数据 新建一个名为insert.sql文件,文件具体内容如下:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;

CREATE TABLE temps(
    name TEXT DEFAULT 'RPi.CPU', 
    tdatetime DATETIME DEFAULT (datetime('now', 'localtime')), 
    temperature NUMERIC NOT NULL
);
    
INSERT INTO temps 
VALUES('RPi.CPU', datetime('now', 'localtime', '-3 hours'), 40.1);

INSERT INTO temps(name, tdatetime, temperature) 
VALUES('RPi.CPU', datetime('now', 'localtime', '-2 hours'), 40.2);

INSERT INTO temps(tdatetime, temperature) 
VALUES(datetime('now', 'localtime', '-1 hours'), 40.3);

INSERT INTO temps(temperature)
VALUES(40.4);

COMMIT;
【简要说明】——若干细节请注意 【1】创建表,表中包括3个字段,分别为name,tdatetime和 temperature。 【2】name DEFAULT 'RPi.CPU',name字段的默认值为'RPi.CPU',SQLite中字符串被单引号包裹。 【3】tdatetime DATETIME DEFAULT (datetime('now', 'localtime')), tdatetime字段默认值为当前时间。 【4】datetime('now', 'localtime')中的localtime表示本时区时间,如果没有该参数则为格林尼治时间。 【5】DEFAULT (datetime('now', 'localtime')), 括号绝对不能少。DEFault中的表达式一定要被括号包裹。 【6】采用多种不同的插入方法,第一种不含字段名称,第二种包含字段名称,第三种,由于创建表格时有默认值,可以使用更简洁的插入方法,例如 INSERT INTO temps(temperature) VALUES(40.4);
2 创建表和插入数据 创建一个名为create-table.sh脚本,具体内容如下#!/bin/shrm -f cpu.dbecho 开始插入数据sqlite3 cpu.db < insert.sqlecho 插入完成 【简要说明】 【1】数据库名称为cpu.db 【2】sqlite3 cpu.db < insert.sql 把insert.sql脚本插入到数据库中,insert.sql包括两个步骤,建立表并向表中插入数据。 【3】修改执行权限 chmod a+x create-tabel.sh,然后执行./create-table.sh 【4】SQLite也有命令行模式,但是命令没有历史查询功能,写错了还要重新写一遍,所有shell脚本指令更利于修改和尝试。
3 查询内容 【简单查询】 新建一个名为show.sh的脚本,具体内容如下#!/bin/shDBNAME="cpu.db"sqlite3 $DBNAME "SELECT * FROM temps;" 【简单说明】 【1】数据库名称为cpu.db。 【2】sqlite3 $DBNAME "select * from temps;" 查询表中所有记录。 【3】修改执行权限 chmod a+x show.sh,然后执行./show.sh 【4】执行结果如下,从结果来看插入的时间间隔一个小时,符合预期效果。RPi.CPU|2014-08-02 17:27:47|40.1RPi.CPU|2014-08-02 18:27:47|40.2RPi.CPU|2014-08-02 19:27:47|40.3RPi.CPU|2014-08-02 20:27:47|40.4
【修改时间顺序】——时间倒序输出#!/bin/shDBNAME="cpu.db"sqlite3 $DBNAME "select * from temps ORDER BY tdatetime DESC;" 【简单说明】 【1】ORDER BY tdatetime DESC 以tdatetime降序排列。 【2】输出结果RPi.CPU|2014-08-02 20:27:47|40.4RPi.CPU|2014-08-02 19:27:47|40.3RPi.CPU|2014-08-02 18:27:47|40.2RPi.CPU|2014-08-02 17:27:47|40.1
【限制时间】——返回最近3个小时的数据#!/bin/shDBNAME="cpu.db"sqlite3 $DBNAME "SELECT * FROM temps where tdatetime > datetime('now', 'localtime', '-3 hours') ORDER BY tdatetime ASC;" 【简要说明】 【1】datetime('now', 'localtime', '-3 hours') 表示当前时间3个小时之前的时间点,一定要加上localtime参数 【2】where tdatetime > datetime('now', 'localtime', '-3 hours') 限制条件3个小时之前到现在。 【3】输出结果如下,特别说明操作的时间约为22:05。RPi.CPU|2014-08-02 19:27:47|40.3RPi.CPU|2014-08-02 20:27:47|40.4
【限制时间】——查询某个时间段内数据#!/bin/shDBNAME="cpu.db"sqlite3 $DBNAME "SELECT * FROM temps where tdatetime > datetime('2014-08-02 19:00:00') and tdatetime <= datetime('2014-08-02 20:00:00');" 【简要说明】 【1】2014-08-02 19:00:00 年必须占4个数字,其他必须占2个数字,不足时使用0不足。 【2】此处不需要增加localtime参数,具体原因未知。
5 总结 【1】创建表格时可使用DEFAULT约束,增加默认值简化插入操作,避免空值。 【2】INSERT操作含有多种方法,根据实际情况选用。 【3】SQLite datetime函数需要指定localtime参数,指定本地时区。
6 参考资料【1】SQLite Date And Time Functions【2】Set up an SQLite database on a Raspberry Pi【3】SQLite column-constraint【4】SQLite 教程 | w3cschool菜鸟教程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: