本文同步自我是一隻香脆的大雞排
sqlite大家不會陌生。Android原生資料庫就是它了。開發過程中我們如果想臨時檢視資料庫中的表結構或內容往往要大費周折的將資料庫拷貝出來然後拿工具開啟檢視,每回都這樣倒騰實在有些麻煩。
實際上在Android Shell下已經有sqlite3環境了,並且足夠滿足基礎的使用。我們來看一下如何使用。
目錄
- 資料庫檔案路徑
- 檢視錶名稱
- 使用sql查詢
- 格式化
- 花式輸出
- 幫助
資料庫檔案路徑
shell下找到你的應用名稱,除非我們額外配置過,否則它們通常放在/data/data/應用包名/databases/
目錄下。
root@p212:/data/data/com.xxx.xxx/databases # ls
Broad.db
okgo.db
複製程式碼
注意如果提示許可權問題,請使用root賬戶。
檢視錶名稱
qlite3 Borad.db .table
qlite3 Borad.db .tables
PassengerEntity RoutingEntity android_metadata
複製程式碼
這樣會列出該資料庫中所有的表。
使用sql查詢
qlite3 Borad.db
qlite3 Borad.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite>
複製程式碼
這樣會進入sqlite的命令下。同時會提示出sqlite的版本,以及輸入.help回車會得到幫助資訊。
我們現在來查詢一條sql。
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
複製程式碼
此時會得到一張很漂亮的表結構資訊。
也就是說,我們如果需要得到這樣的表結構,我們依次需要輸入
- qlite3 xxx.db //開啟資料庫
- .header on //啟用表頭
- .mode column //使用列模式
- select * from xxx //查詢某張表
才可以得到漂亮的資料。吐槽一句,sqlite的這種設計有點愚蠢,為什麼不是一開始就預設啟用表頭和列模式呢?
花式輸出
除了column模式以外,其實還內建了幾組不同的模式。分別是:
- ascii
- csv
- column
- html
- insert
- line
- list
- tabs
- tcl
我們試一下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
複製程式碼