sqlite3常用命令

释放双眼,带上耳机,听听看~!

sqlite3常用命令

当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识:
#sqlite3 test.db

查看数据库文件信息命令(注意命令前带字符'.'):
sqlite>.database
查看所有表的创建语句:
sqlite>.schema

查看指定表的创建语句:
sqlite>.schema table_name

以sql语句的形式列出表内容:
sqlite>.dump table_name

设置显示信息的分隔符:
sqlite>.separator symble
Example:设置显示信息以‘:’分隔
sqlite>.separator :
设置显示模式:
sqlite>.mode mode_name
Example:默认为list,设置为column,其他模式可通过.help查看mode相关内容
sqlite>.mode column

输出帮助信息:
sqlite>.help

设置每一列的显示宽度:
sqlite>.width width_value
Example:设置宽度为2
sqlite>.width 2

列出当前显示格式的配置:
sqlite>.show

退出sqlite终端命令:
sqlite>.quit

sqlite>.exit
3、sqlite3指令
sql的指令格式:所有sql指令都是以分号(;)结尾,两个减号(–)则表示注释。
如:
sqlite>create studen_table(Stu_no interger PRIMARY KEY, Name text NOT NULL, Id interger UNIQUE, Age interger CHECK(Age>6), School text DEFAULT 'xx小学);
该语句创建一个记录学生信息的数据表。

3.1 sqlite3存储数据的类型
NULL:标识一个NULL值
INTERGER:整数类型
REAL:浮点数
TEXT:字符串
BLOB:二进制数
3.2 sqlite3存储数据的约束条件
Sqlite常用约束条件如下:
PRIMARY KEY – 主键:
1)主键的值必须唯一,用于标识每一条记录,如学生的学号
2)主键同时也是一个索引,通过主键查找记录速度较快
3)主键如果是整数类型,该列的值可以自动增长
NOT NULL – 非空:
约束列记录不能为空,否则报错
UNIQUE – 唯一:
除主键外,约束其他列的数据的值唯一
CHECK – 条件检查:
约束该列的值必须符合条件才可存入
DEFAULT – 默认值:
列数据中的值基本都是一样的,这样的字段列可设为默认值
3.3 sqlite3常用指令
1)建立数据表
create table table_name(field1 type1, field2 type1, …);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);
2)添加数据记录
insert into table_name(field1, field2, …) values(val1, val2, …);
valx为需要存入字段的值。
例,往学生信息表添加数据:
Insert into student_info(stu_no, name) values(0001, alex);

3)修改数据记录
update table_name set field1=val1, field2=val2 where expression;
where是sql语句中用于条件判断的命令,expression为判断表达式
例,修改学生信息表学号为0001的数据记录:
update student_info set stu_no=0001, name=hence where stu_no=0001;

4)删除数据记录
delete from table_name [where expression];
不加判断条件则清空表所有数据记录。
例,删除学生信息表学号为0001的数据记录:
delete from student_info where stu_no=0001;
5)查询数据记录
select指令基本格式:
select columns from table_name [where expression];
a查询输出所有数据记录
select * from table_name;
b限制输出数据记录数量
select * from table_name limit val;
c升序输出数据记录
select * from table_name order by field asc;
d降序输出数据记录
select * from table_name order by field desc;
e条件查询
select * from table_name where expression;
select * from table_name where field in ('val1', 'val2', 'val3');
select * from table_name where field between val1 and val2;
f查询记录数目
select count (*) from table_name;
g区分列数据
select distinct field from table_name;

有一些字段的值可能会重复出现,distinct去掉重复项,将列中各字段值单个列出。

h
查询莫一行数据的前后几行

前十行:select * from
table_name where
field<5 order by
field asc limit 10;

后十行:select * from
table_name where
field

5 order by

field
desc limit 10;

6)建立索引
当说数据表存在大量记录,索引有助于加快查找数据表速度。
create index index_name on table_name(field);
例,针对学生表stu_no字段,建立一个索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在对该字段查询时,会自动使用该索引。

7)删除数据表或索引
drop table table_name;
drop index index_name;

8
)导入数据:
.read 
 

数据文件

打开记事本,并将下列 SQL 语句复制到记事本中,保存为 test.sql 到上面说到的 Db目录下,在命令行环境中输入

.read  
test.sql

即将所有的数据导入到 test.db 数据库中。
BEGIN TRANSACTION;
Insert into student_info(stu_no, name) values(0003, "2alex");
Insert into student_info(stu_no, name) values(0004, "2alex");
Insert into student_info(stu_no, name) values(0005, "2alex");
Insert into student_info(stu_no, name) values(0006, "2alex");
Insert into student_info(stu_no, name) values(0007, "2alex");
Insert into student_info(stu_no, name) values(0008, "2alex");
Insert into student_info(stu_no, name) values(0009, "2alex");
Insert into student_info(stu_no, name) values(0010, "2alex");
Insert into student_info(stu_no, name) values(0011, "2alex");
Insert into student_info(stu_no, name) values(0012, "2alex");
COMMIT;

9)
.explain ?ON|OFF?
开启或关闭适合于的输出模式。不指定ON或OFF时,默认为ON。

android使用的是sqlite数据库,sqlite是比较轻量级的数据库,在Google了之后发现,sqlite事务处理的问题,在sqlite插入数据的时候默认一条语句就是一个事务,有多少条数据就有多少次磁盘操作。我的应用初始5000条记录也就是要5000次读写磁盘操作。

解决方法:

添加事务处理,把5000条插入作为一个事务

dataBase.beginTransaction();       //手动设置开始事务

//数据插入操作循环

dataBase.setTransactionSuccessful();       //设置事务处理成功,不设置会自动回滚不提交

dataBase.endTransaction();       //处理完成

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

详解Node.js API系列 Crypto加密模块(2) Hmac

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索