本文同步自我是一只香脆的大鸡排html
sqlite你们不会陌生。Android原生数据库就是它了。开发过程当中咱们若是想临时查看数据库中的表结构或内容每每要大费周折的将数据库拷贝出来而后拿工具打开查看,每回都这样倒腾实在有些麻烦。android
实际上在Android Shell下已经有sqlite3环境了,而且足够知足基础的使用。咱们来看一下如何使用。sql
shell下找到你的应用名称,除非咱们额外配置过,不然它们一般放在/data/data/应用包名/databases/
目录下。shell
root@p212:/data/data/com.xxx.xxx/databases # ls
Broad.db
okgo.db
复制代码
注意若是提示权限问题,请使用root帐户。数据库
qlite3 Borad.db .table
bash
qlite3 Borad.db .tables
PassengerEntity RoutingEntity android_metadata
复制代码
这样会列出该数据库中全部的表。工具
qlite3 Borad.db
ui
qlite3 Borad.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite>
复制代码
这样会进入sqlite的命令下。同时会提示出sqlite的版本,以及输入.help回车会获得帮助信息。this
咱们如今来查询一条sql。spa
sqlite> select * from RoutingEntity;
sqlite> select * from RoutingEntity;
1|2|2|1518247150757|1|1
2|2|2|1518247168420|1|1
复制代码
这样就获得了RoutingEntity表中内容了。注意务必使用标准的sql结束符。(就是分号)
很快咱们发现没有表头看不清每行数据的意思。咱们可使用。
.header on
后再一次查询:
sqlite> .header on
sqlite> select * from RoutingEntity;
id|current_num|max_num|time|warning_num|type
1|2|2|1518247150757|1|1
2|2|2|1518247168420|1|1
sqlite>
复制代码
就会获得带表头的结果,可是很快又会发现表头没有对齐。
再输入:
.mode column
后再一次查询:
sqlite> .mode column
sqlite> select * from RoutingEntity;
id current_num max_num time warning_num type
---------- ---------- ---------- ------------- ---------- ----------
1 2 2 1518247150757 1 1
2 2 2 1518247168420 1 1
复制代码
此时会获得一张很漂亮的表结构信息。
也就是说,咱们若是须要获得这样的表结构,咱们依次须要输入
才能够获得漂亮的数据。吐槽一句,sqlite的这种设计有点愚蠢,为何不是一开始就默认启用表头和列模式呢?
除了column模式之外,其实还内置了几组不一样的模式。分别是:
咱们试一下html模式
sqlite> .mode html
sqlite> select * from RoutingEntity;
<TR><TH>id</TH>
<TH>current_num</TH>
<TH>max_num</TH>
<TH>time</TH>
<TH>warning_num</TH>
<TH>type</TH>
</TR>
<TR><TD>1</TD>
<TD>2</TD>
<TD>2</TD>
<TD>1518247150757</TD>
<TD>1</TD>
<TD>1</TD>
</TR>
<TR><TD>2</TD>
<TD>2</TD>
<TD>2</TD>
<TD>1518247168420</TD>
<TD>1</TD>
<TD>1</TD>
</TR>
复制代码
哇哦,有点炸裂。
再来一组tcl。
sqlite> .mode tcl
sqlite> select * from RoutingEntity;
"id" "current_num" "max_num" "time" "warning_num" "type"
"1" "2" "2" "1518247150757" "1" "1"
"2" "2" "2" "1518247168420" "1" "1 复制代码
csv
sqlite> .mode csv
sqlite> select * from RoutingEntity;
id,current_num,max_num,time,warning_num,type
1,2,2,1518247150757,1,1
2,2,2,1518247168420,1,1
复制代码
若是咱们想知道更多的用法,可使用 .help来打开查看。
sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail on|off Stop after hitting an error. Default OFF
.binary on|off Turn binary output on or off. Default OFF
.clone NEWDB Clone data into NEWDB from the existing database
.databases List names and files of attached databases
.dbinfo ?DB? Show status information about the database
.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
.eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN
.exit Exit this program
.explain ?on|off? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.fullschema Show schema and the content of sqlite_stat tables
.headers on|off Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indexes ?TABLE? Show names of all indexes
If TABLE specified, only show indexes for tables
matching LIKE pattern TABLE.
.limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
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 strings
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Use STRING in place of NULL values
.once FILENAME Output for the next SQL command only to FILENAME
.open ?FILENAME? Close existing database and reopen FILENAME
.output ?FILENAME? Send output to FILENAME or stdout
.print STRING... Print literal STRING
.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
.save FILE Write in-memory database into FILE
.scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator COL ?ROW? Change the column separator and optionally the row
separator for both the output mode and .import
.shell CMD ARGS... Run CMD ARGS... in a system shell
.show Show the current values for various settings
.stats on|off Turn stats on or off
.system CMD ARGS... Run CMD ARGS... in a system shell
.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
.timer on|off Turn SQL timer on or off
.trace FILE|off Output each SQL statement as it is run
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set column widths for "column" mode
Negative values right-justify
复制代码