本文同步自我是一隻香脆的大雞排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
複製代碼