Mac通過終端查看Sqlite

平時查看數(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ì)的命令附在最后。

有幾點需要注意一下:

  1. 終端操作sqlite的命令都是通過.*這種方式。注意和一把命令的區(qū)別。
  2. sql語句每一句結(jié)尾都需要用分號才會起作用
  3. 特別注意??!一定要注意輸入,建議在寫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)該能夠看懂。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • 今天,似乎是個不太好的日子。 我是涼憶。 今天是我弟打預(yù)防針的日子。我提心吊膽地抱著我弟,走進(jìn)疫苗接種大廳。不一會...
    千江月涼憶閱讀 202評論 0 1
  • 春天的腳步已經(jīng)越來越近了。每天去上班路過小區(qū)門口的柳樹,都會發(fā)現(xiàn)新芽在一天天地往外露尖。路上的行人也都脫去了身上厚...
    像個孩子520閱讀 394評論 0 2
  • 最近看到關(guān)于婆媳關(guān)系的文章,也接觸到當(dāng)今年輕人主要是80后90初面臨的一些家庭問題,婚姻矛盾,簡單做一分析。 丈夫...
    易簡堂主閱讀 624評論 0 1

友情鏈接更多精彩內(nèi)容