SQLite学习笔记
参考书籍 《SQLite 权威指南 第二版》
Windows获取SQLite
1.主页: www.sqlite.org
2.下载 Precompiled Binaries For Windows
3.设置系统环境PATH
使用
打开cmd,输入sqlite3
命令 .help 帮助
.exit 退出程序
数据库管理
//创建数据库,
sqlite3 test.db
//创建一个表,此时才会创建数据库
create table test (id integer primary key, value text);
//向表中插入几行数据
insert into test (id ,value ) values(1,'eenie');
insert into test (id ,value ) values(2,'meenie');
insert into test (value) values ('miny');
insert into test (value) values ('mo');
//返回插入内容
.mode column
.headers on
select * from test;
//获得最后插入的自动增量值
select last_insert_rowid();
//添加一个索引和视图
create index test_idx on test (value);
create view schema as select *from sqlite_master;
//退出
.exit
获得数据库的Schema信息
//返回所有表和视图
sqlite> .tables
//显示一个表的索引
sqlite> .indices test
//返回所有数据库对象的创建语句
sqlite> .schema
//返回特定数据库对象的创建语句
sqlite> .schema test
//查询当前数据库的sqlite_master表
//首先设置字段格式和标题
sqlite> .mode col
sqlite> .headers on
sqlite> select type,name,tbl_name,sql from sqlite_master order by types;
sqlite_master 表结构 |
||
编号 |
字段名 |
说明 |
1 |
type |
对象类型(table,index,trigger,view) |
2 |
name |
对象名称 |
3 |
tbl_name |
对象关联的表 |
4 |
Rootpage |
对象根页面在数据库的索引(开始编号) |
5 |
sql |
对象的SQL定义(DDL) |
导出数据
sqlite> .output file.sql
sqlite> .dump
sqlite> .output stdout
导入数据
1.导入sql语句
sqlite> .mode column
sqlite> .headers on
sqlite> select * from test; //移除已经存在的数据库对象(test表和schema视图)
sqlite> drop table test;
sqlite> drop view schema; sqlite> select * from test;
//导入sql语句
sqlite> .read file.sql sqlite> select * from test;
2.import 导入数据 sqlite> .import [file][table]
格式化
//改变CLP的Shell提示符
sqlite> .prompt 'sqlite3>' //.mode 命令可以设置结果数据的几种输出格式
//csv,column,html,insert,line,list,tabs,tcl
//默认值 list //CSV格式输出一个表的数据
sqlite3> .output file.csv
sqlite3> .mode csv
sqlite3> .separator ,
sqlite3> select * from test;
sqlite3> .output stdout
导出带分隔符的数据
//导出test表中以字母m开始的值,并以逗号分隔
sqlite> .output test.csv
sqlite> .separator ,
sqlite> select * from test where value like 'm%';
sqlite> .output stdout //将CSV数据导入到与test表结构体类似的表(test2)中
sqlite> create table test2(id integer primary key,value text);
sqlite> .import text.csv test2
执行无人值守维护
1.提供SQL命令或SQLite shell命令 如 .dump和.schema
//从命令行转存test.db数据库
sqlite3 test.db .dump >test.sql
//从test.sql中创建新的数据库test2.db
sqlite3 test2.db < test.sql
2.使用init选项
sqlite3 -init test.sql test3.db .exit
备份数据库
1.命令行
sqlite3 test.db .dump > test.Sql
2.Shell中 如 导出数据
3.备份二进制数据库(二进制文件没有备份SQL移植性好)
sqlite3 test.db vacuum
cp test.db test.Backup(Linux)
书中例子:
运行示例
sqlite3 foods.db < test.sql //使数据内容更具有可读性
.echo on
.mode column
.headers on
.nullvalue NULL //查找
sqlite> select *
...> from foods
...> where name = 'JujyFruit'
...> and type_id = 9; //对较长的查询,我们以代码形式显示SQL
select f.name name,types.name type
from foods f
inner join (
select *
from food_types
where id=6) types
on f.type_id = types.id;
语法
select id from foods where name = 'JujyFruit'
动词 主语 谓语
命令 SQl由命令组成,每条命令以分号(;)结束
常量 constants 表示确切的值 3种类型
字符串常量、数字常量和二进制常量
如果字符串本身包含单引号 kenny's chicken -> 'kenny''s chicken'
关键字和标识符 字符常量值 -> 大小写敏感
注释 -- 单行注释
/* */ 多行注释
创建数据库
创建表 create [temp|temporary] table table_name ( column_definitions [,constranits=]);
修改表 alter table table { rename to name | add column column_def }
数据库查询
关系操作
基本操作
- Restriction (限制)
- Projection(投影)
- Cartesian Product(笛卡尔积)
- Union(联合)
- Different(差)
- Rename(重命名)
附加操作
- Intersection(交叉)
- Natural Join(自然连接)
- Assign(赋值)
扩展操作
- Generalized Projection(广义投影)
- Left Outer Join(左外连接)
- Right Outer Join(右外连接)
- Full Outer Join(全外连接)
例如 select name from (select name,type_id from (select * from foods));
select 命令与操作管道
过滤
select * from dogs where color = 'purple' and grin = 'toothy';
值:代表了真是世界的某种数据
操作符
二元操作符
逻辑操作符
LIKE与GLOB操作符
LIKE的作用与相等(=)
查询表foods中所有名称以字符“J”开始的食品
select id,name from foods where name like 'J%';
模式中百分号(%)可与任意0个或多个字符匹配
百分号是贪婪匹配,在最左边或者最右边(将匹配字符的另外一边)
下画线(_)可以与任意单个字符匹配
select id , name from foods where name like '%ac'
select id , name from foods where name like '%ac%'
select id , name from foods where name like '%ac%p%'
使用NOT否定某些模式
select id , name from foods where name like '%ac%P%' and name not like '%Sch%'
GLOB 有些像UNIX/Linux
select id ,name from foods where name glob 'Pnie*';
SQLite也识别match和regexp正则表达式断言
目前不提供自身实现
开发自己的sqlite_create_function()调用
限定和排序
limit和offset 关键字限定结果集的大小和范围
limit 数据数量 offset 起点位置
select * from food_types order by id limit 1 offset 1;
order by 使记录集在返回之前按一个或多个字段的值进行排序
asc(默认的升序)或desc(降序)
select * from foods where name like 'B%' order by type_id desc, name limit 10;
SQLite学习笔记的更多相关文章
- SQLite学习笔记(七)&&事务处理
说到事务一定会提到ACID,所谓事务的原子性,一致性,隔离性和持久性.对于一个数据库而言,通常通过并发控制和故障恢复手段来保证事务在正常和异常情况下的ACID特性.sqlite也不例外,虽然简单,依然 ...
- Sqlite学习笔记(四)&&SQLite-WAL原理
Sqlite学习笔记(三)&&WAL性能测试中列出了几种典型场景下WAL的性能数据,了解到WAL确实有性能优势,这篇文章将会详细分析WAL的原理,做到知其然,更要知其所以然. WAL是 ...
- Sqlite学习笔记(四)&&SQLite-WAL原理(转)
Sqlite学习笔记(三)&&WAL性能测试中列出了几种典型场景下WAL的性能数据,了解到WAL确实有性能优势,这篇文章将会详细分析WAL的原理,做到知其然,更要知其所以然. WAL是 ...
- SQLite 学习笔记
SQLite 学习笔记. 一.SQLite 安装 访问http://www.sqlite.org/download.html下载对应的文件. 1.在 Windows 上安装 SQLite. ...
- sqlite学习笔记7:C语言中使用sqlite之打开数据库
数据库的基本内容前面都已经说得差点儿相同了.接下看看如何在C语言中使用sqlite. 一 接口 sqlite3_open(const char *filename, sqlite3 **ppDb) 打 ...
- Sqlite学习笔记(一)&&编译安装
Sqlite简介 sqlite是一个开源的嵌入式文件数据库,sqlite以动态链接库的方式供应用程序调用,所有的数据库对象都存储在同一个文件中. sqlite动态库非常小,最新的3.8.11版本也只有 ...
- SQLite学习笔记(十)&&加密
随着移动互联网的发展,手机使用越来越广泛,sqlite作为手机端存储的一种解决方案,使用也非常普遍.但是sqlite本身安全特性却比较弱,比如不支持用户权限,只要能获取到数据库文件就能进行访问:另外也 ...
- SQLite学习笔记(十二)&&虚拟机指令
上篇文章简单讨论了虚拟机的原理,这篇文章我们详细讨论下指令,具体从几种典型的SQL语句来看看每种SQL对应的指令流,以及每个指令的含义.通过explain语句,可以看到语句对应的指令流:通过pragm ...
- SQLite学习笔记(十一)&&虚拟机原理
前言 我们知道任何一种关系型数据库管理系统都支持SQL(Structured Query Language),相对于文件管理系统,用户不用关心数据在数据库内部如何存取,也不需要知道底层的存储 ...
- SQLite学习笔记(九)&&pager模块
概述 通过上一篇文章的分析,我们知道了pager模块在整个sqlite中所处的位置.它是sqlite的核心模块,充当了多种重要角色.作为一个事务管理器,它通过并发控制和故障恢复实现事务的ACID特性, ...
随机推荐
- 微软操作系统 Windows Server 2012 R2 官方原版镜像
微软操作系统 Windows Server 2012 R2 官方原版镜像 Windows Server 2012 R2 是由微软公司(Microsoft)设计开发的新一代的服务器专属操作系统,其核心版 ...
- js数组 标签: javascript 2016-08-03 14:15 131人阅读 评论(0) 收藏
数组排序 reverse()方法 reverse()方法会反转数组的顺序. sort()方法 默认情况下sort()方法按升序排列数组项.为实现排序sort()方法调用每项的toString(),然后 ...
- ZT JAVA WeakReference
JAVA WeakReference 分类: JAVA 2012-08-28 16:08 305人阅读 评论(0) 收藏 举报 javareferencehashmapcacheclassnull 在 ...
- Yii: 扩展CGridView增加导出CSV功能
Yii提供的CGridView组件没有内置数据导出功能,不过我们可以通过扩展该组件来添加该功能. 具体方法如下: 1.首先派生一个子类,添加一个action成员,在该视图的init函数中判断是浏览动作 ...
- StringUtils工具类介绍
1 abbreviate方法缩写一段文字 StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg...& ...
- 【HAOI2010】工厂选址题解
题目描述 某地区有m座煤矿,其中第i号矿每年产量为ai吨,现有火力发电厂一个,每年需用煤b吨,每年运行的固定费用(包括折旧费,不包括煤的运费)为h元,每吨原煤从第i号矿运到原有发电厂的运费为Ci0(i ...
- luogu P3801 红色的幻想乡
嘟嘟嘟 首先人人都能想到是线段树,不过二维线段树肯定会MLE+TLE的. 我们换一种想法,不去修改整个区间,而是修改一个点:开横竖两个线段树,分别记录哪些行和列被修改了.因为如果两阵红雾碰撞,则会因为 ...
- new ,malloc
特征 new/delete malloc/free 分配内存的位置 自由存储区 堆 内存分配失败返回值 完整类型指针 void* 内存分配失败返回值 默认抛出异常 返回NULL 分配内存的大小 由编译 ...
- 1spring注解:@Configuration,@Bean,@ComponentScan(),@Scope
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Unity透明视频播放 所需的Shader脚本
Shader "Custom/ShaderMovie" { Properties { _MainTex("Color (RGB)", 2D) = "b ...