sqlite3常用命令&语法

2018-02-27  本文已影响0人  lenozhao

常用命令

新建(打开)数据库

sqlite3 数据库名 如:

# sqlite3 user_setting.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

检查databasefilename是否存在
如果不存在就创建并进入数据库,如果直接退出(即执行 .exti ),数据库文件不会创建
如果已经存在直接进入数据库,对数据库进行操作

显示数据库信息

.database 如:

sqlite> .database
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /tvdatabase/Database/user_setting.db                      
sqlite>

显示表名称

.table 或者 .tables

sqlite> .tables 
tbl_AbbRatingText                          
tbl_AndroidConfig                          
tbl_BlockSysSetting                        
tbl_BootSetting                            
.......    

查看创建数据库对象时 SQL 语句

.schema

sqlite> .schema
CREATE TABLE [tbl_3DInfo] (
[_id] INTEGER  NOT NULL PRIMARY KEY,
[bEnable3D] INTEGER NULL,
[enInput3DMode] INTEGER NULL,
[enOutput3DMode] INTEGER NULL
);
CREATE TABLE [tbl_3DSetting] (
[_id] INTEGER  PRIMARY KEY NOT NULL,
[enDisplayMode] INTEGER  NULL,
[en3DFormat] INTEGER  NULL,
[en2DFormat] INTEGER  NULL,
[enAutoStart] INTEGER  NULL,
[en3DTimerPeriod] INTEGER  NULL
);
.......

是否显示表头

.headers on/off

sqlite> .headers on
sqlite> select * from tbl_MiscSetting;
_id         MTSSetting  BlockUnratedTV  CurrentTVtype
----------  ----------  --------------  -------------
0           1           0               0 

sqlite> .headers off
sqlite> select * from tbl_MiscSetting;
0           1           0               0 

改变输出格式

.mode list|column|insert|line|tabs|tcl|csv|html

更改分界符

.separator "分界符"
sqlite> .separator "==" sqlite> select * from tbl_MiscSetting; 0==1==0==0

dump

output

设置输出的 NULL 字符串

.nullvalue STRING 查询时用指定的串代替输出的NULL串 默认为.nullvalue ''

退出

.exit 如:

sqlite> .exit

查看帮助

.help 如:

sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices ?TABLE?       Show names of all indices
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML <table> code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by .separator string
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Print STRING in place of NULL values
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.vfsname ?AUX?         Print the name of the VFS stack
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off

字段类型

数据库中存储的每个值都有一个类型,都属于下面所列类型中的一种,(被数据库引擎所控制)

值被定义为什么类型只和值自身有关,和列没有关系,和变量也没有关系.所以sqlite被称作 弱类型 数据库
数据库引擎将在执行时检查、解析类型,并进行数字存储类型(整数和实数)和文本类型之间的转换.

但实际上,sqlite3也接受如下的数据类型:

常用函数

时间/日期函数

算术函数

sqlite> select max(2,3,4,5,6,7,12);
12

字符处理函数

条件判断函数

ifnull(X,Y) 如果X为null 返回Y

select ifnull(comm,0) from emp;
0
300
500
0
1400

集合函数

其他函数

基本语法

insert into table_name values (field1, field2, field3...);
select * from table_name;查看table_name表中所有记录;

select * from table_name where field1='xxxxx'; 查询符合指定条件的记录;

select ..... 
from table_name[,table_name2,...]
where ..... 
group by.... 
having .... 
order by ...

select ..... 
from table_name  inner join | left outer join | right outer join table_name2
on ...
where ..... 
group by.... 
having .... 
order by ...
select * from EMP m where SAL > (select avg(SAL) from EMP where DEPTNO=m.DEPTNO);  
update EMP
set SAL=
(
case
when DEPTNO=10 and JOB='MANAGER' then SAL*1.1
when DEPTNO=20 and JOB='CLERK' then SAL*1.2
when DEPTNO=30  then SAL*1.1
when DEPTNO=40  then SAL*1.2
else SAL
END
);

select ENAME, 
case DEPTNO
when 10 then '后勤部'
when 20 then '财务部'
when 30 then '内务部门'
else '其他部门'
end as dept
from EMP;
select *
from emp e
where e.EMPNO in 
(
select empno  
from EMP
where deptno=e.DEPTNO
order by SAL desc
limit 0,2
);
select deptno from emp
union 
select deptno from dept

select deptno from emp
union all
select deptno from dept;

在列名前加distinct也是去重复

sqlite> select distinct deptno from emp;
delete from table_name where ...
drop table_name;     删除表;
drop index_name;     删除索引;
update table_name
set xxx=value[, xxx=value,...]
where ...
create index film_title_index on film(name);

意思是针对film资料表的name字段,建立一个名叫film_name_index的索引。这个指令的语法为

CREATE [ UNIQUE ]  NONCLUSTERED  INDEX index_name
    ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
create index index_name on table_name(field_to_be_indexed);

其他sqlite的特别用法

参考 http://blog.csdn.NET/linchunhua/article/details/7184439

上一篇下一篇

猜你喜欢

热点阅读