Mac通过终端查看Sqlite

平时查看数据库内容大部分时间都是用的IDE,比如Navicate。Navicate比较重量级,写服务端的时候必不可少。对于sqlite而言也有一些轻量级的IDE。不过这都不是今天的重点。今天介绍的方式是不通过IDE的情况下,如何在终端查看sqlite数据库。

今天再给另一个同事调试问题的时候发现他机子上没安装相关查看的sqlite软件,所以才有此文。开始吧!

前提是你需要懂一些sql语句,对于一个程序员而言,这个应该没有丝毫难度。

基本介绍

苹果开发者文档里面其实就有这方面的介绍。所以如果英文还可以的同学建议直接看官方文档。Sqlite Man Page

在不知道某些终端命令的情况下,建议用-help或者man这种方式来查看提示。比如:

《Mac通过终端查看Sqlite》

详细的命令附在最后。

有几点需要注意一下:

  1. 终端操作sqlite的命令都是通过.*这种方式。注意和一把命令的区别。
  2. sql语句每一句结尾都需要用分号才会起作用
  3. 特别注意!!一定要注意输入,建议在写sql的时候完全切换到英文状态。

常用命令

在iOS使用而言最多的应该算是查询。那么久首先看看查询。

  • 打开数据库:sqlite3 + 数据库路径

    《Mac通过终端查看Sqlite》

  • 查看数据库表有哪些:.tables

    《Mac通过终端查看Sqlite》

  • 选择记录:其实就是常用的sql语句的使用了,再次提醒,一定要记到是在英文输入法环境下,因为终端空格是察觉不出来是英文还是中文环境下。:select * from web_track limit 1;

《Mac通过终端查看Sqlite》

退出: .quit;

除此之外,其实其余的就和使用平时用的sql语句一模一样。

不常用的命令

命令含义实例
.show显示格式的配置情况(一会我们显示表中数据的时候就会看到效果), 《Mac通过终端查看Sqlite》
.schema建表的模式(就是建表语句,个人观点) 《Mac通过终端查看Sqlite》
.mode显示的格式,建议用line分行显示,首先输入查询语句 《Mac通过终端查看Sqlite》
.headers on显示字段名,注意在column模式才有用 《Mac通过终端查看Sqlite》

其余的命令自行摸索吧!!

命令附录

.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

英文不错的你应该能够看懂。

    原文作者:纸简书生
    原文地址: https://www.jianshu.com/p/33bec60cfbaa
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞