平時查看數(shù)據(jù)庫內(nèi)容大部分時間都是用的IDE,比如Navicate。Navicate比較重量級,寫服務(wù)端的時候必不可少。對于sqlite而言也有一些輕量級的IDE。不過這都不是今天的重點。今天介紹的方式是不通過IDE的情況下,如何在終端查看sqlite數(shù)據(jù)庫。
今天再給另一個同事調(diào)試問題的時候發(fā)現(xiàn)他機子上沒安裝相關(guān)查看的sqlite軟件,所以才有此文。開始吧!
前提是你需要懂一些sql語句,對于一個程序員而言,這個應(yīng)該沒有絲毫難度。
基本介紹
蘋果開發(fā)者文檔里面其實就有這方面的介紹。所以如果英文還可以的同學(xué)建議直接看官方文檔。Sqlite Man Page
在不知道某些終端命令的情況下,建議用-help或者man這種方式來查看提示。比如:

詳細(xì)的命令附在最后。
有幾點需要注意一下:
- 終端操作sqlite的命令都是通過
.*這種方式。注意和一把命令的區(qū)別。 - sql語句每一句結(jié)尾都需要用分號才會起作用
- 特別注意??!一定要注意輸入,建議在寫sql的時候完全切換到英文狀態(tài)。
常用命令
在iOS使用而言最多的應(yīng)該算是查詢。那么久首先看看查詢。
-
打開數(shù)據(jù)庫:sqlite3 + 數(shù)據(jù)庫路徑
-
查看數(shù)據(jù)庫表有哪些:.tables
- 選擇記錄:其實就是常用的sql語句的使用了,再次提醒,一定要記到是在英文輸入法環(huán)境下,因為終端空格是察覺不出來是英文還是中文環(huán)境下。:select * from web_track limit 1;

退出: .quit;
除此之外,其實其余的就和使用平時用的sql語句一模一樣。
不常用的命令
| 命令 | 含義 | 實例 |
|---|---|---|
| .show | 顯示格式的配置情況(一會我們顯示表中數(shù)據(jù)的時候就會看到效果), | ![]() |
| .schema | 建表的模式(就是建表語句,個人觀點) | ![]() |
| .mode | 顯示的格式,建議用line分行顯示,首先輸入查詢語句 | ![]() |
| .headers on | 顯示字段名,注意在column模式才有用 | ![]() |
其余的命令自行摸索吧!!
命令附錄
.auth ON|OFF Show authorizer callbacks
.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
.changes on|off Show number of rows changed by SQL
.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|full Enable or disable automatic EXPLAIN QUERY PLAN
.exit Exit this program
.explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic
.fullschema ?--indent? 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 ?PATTERN? Show the CREATE statements matching PATTERN
Add --indent for pretty-printing
.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? Show stats or 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
.vfsinfo ?AUX? Information about the top-level VFS
.vfslist List all available VFSes
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set column widths for "column" mode
Negative values right-justify
英文不錯的你應(yīng)該能夠看懂。





