android sqlite

版权声明:如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!本文为博主原创文章,转载请注明链接! https://blog.csdn.net/tfygg/article/details/53240046

一、sqlite常用命令

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

#sqlite3 test.db


可以查询当前数据库中所有的表名

.table


查看当前数据库文件信息命令(注意命令前带字符'.'):

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


二、sqlite常用语句

2.1 存储数据的类型
NULL:标识一个NULL值
INTERGER:整数类型
REAL:浮点数
TEXT:字符串
BLOB:二进制数

2.2 语句
1)建立数据表
create table table_name(field1 type1, field2 type1, ...);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);


create table if not exists 表名(字段名1,字段名2...);


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去掉重复项,将列中各字段值单个列出。

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;



猜你喜欢

转载自blog.csdn.net/tfygg/article/details/53240046