文章參照:www.sqlite.org/sqlite.html在sqlite3中我們可以通過".help"命令來查所有的非SQL命令。比如,樣本4:
sqlite> .help.help.backup ?DB? FILE Backup DB (default "main") to FILE.bail ON|OFF Stop after hitting an error. Default OFF.databases List names and files of attached databases.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.exit Exit this program.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off. With no args, it turns EXPLAIN on..genfkey ?OPTIONS? Options are: --no-drop: Do not drop old fkey triggers. --ignore-errors: Ignore tables with fkey errors --exec: Execute generated SQL immediately See file tool/genfkey.README in the source distribution for further information..header(s) ON|OFF Turn display of headers on or off.help Show this message.import FILE TABLE Import data from FILE into TABLE.indices ?TABLE? Show names of all indices If TABLE specified, only show indices for tables matching LIKE pattern TABLE..load FILE ?ENTRY? Load an extension library.log FILE|off Turn logging on or off. FILE can be stderr/stdout.mode MODE ?TABLE? Set output mode where MODE is one of: 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 string tabs Tab-separated values tcl TCL list elements.nullvalue STRING Print STRING in place of NULL values.output FILENAME Send output to FILENAME.output stdout Send output to the screen.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.schema ?TABLE? Show the CREATE statements If TABLE specified, only show tables matching LIKE pattern TABLE..separator STRING Change separator used by output mode and .import.show Show the current values for various settings.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.width NUM1 NUM2 ... Set column widths for "column" mode.timer ON|OFF Turn the CPU timer measurement on or offsqlite>sqlite3能以8種不同的方式顯示查詢返回的結果:
"csv
", "column",
"html", "insert", "line",
"list","tabs", "tcl".
你可以通過.mode來設定顯示的方式。預設的是"list"方式,這時返回結果的一條記錄顯示一行,每列的內容之間用設定的分隔字元隔開,預設的分隔字元是"|".樣本5:sqlite> .mode listsqlite> select * from tbl1;hello|10goodbye|20sqlite>你可以通過".separator"來設定"list"模式下的分隔字元。比如我們想把", "作為分隔字元,可以這樣:樣本6:sqlite> .separator ", "sqlite> select * from tbl1;hello, 10goodbye, 20sqlite>"line"模式下, 每行只顯示資料庫的一行的一列。每行由列名,等號和列的值組成。每條記錄之間由一個空行隔開。比如,樣本7sqlite> .mode linesqlite> select * from tbl1;one = hellotwo = 10
one = goodbyetwo = 20sqlite>
"column"模式下,每條記錄都在單獨一行顯示。第一行顯示的是列名,第二行只是用於分割列名和記錄的資料,第三行開始才是記錄的內容。比如,樣本8:
sqlite> .mode column
sqlite> select * from tbl1;one two ---------- ----------hello 10 goodbye 20 sqlite>預設情況下,每列的寬度最多隻能顯示10個字元。所以如果資料包含的字元如果他太多,可能就顯示不完。但是我們可以通過".width"設定每列的寬度。比如,樣本9:sqlite> .width 12 6sqlite> select * from tbl1;one two ------------ ------hello 10 goodbye 20 sqlite>樣本9就把第1列和第二列的寬度分別設定為了12和6個字元,其他列的寬度並沒改變。如果你把列寬設定為0,那麼調整為以下三個的最大值:10,該列的列名字元數,第一行記錄該列的字元數。這樣列的寬度就可以自動調整。預設的列寬就是0,以便它可以自動調整。可以通過".header"命令可以設定是否顯示頭(頭包括第一行,列名,第二行,分隔行)。比如,樣本10:sqlite> .header offsqlite> select * from tbl1;hello 10 goodbye 20 sqlite>"insert"模式下,返回的查詢結果將以SQL的INSERT語句形式進行顯示。比如,樣本11:sqlite> .mode insert new_tablesqlite> select * from tbl1;INSERT INTO 'new_table' VALUES('hello',10);INSERT INTO 'new_table' VALUES('goodbye',20);sqlite>"html"模式下,查詢返回結果將以XHTML table的形式進行顯示,它並不以<TABLE>作為開頭和</TABLE>作為結尾。 但是每條記錄以<TR>作為開始,</TR>作為結束,記錄的資料以<TD>作為開始,以</TD>作為結束比如,樣本12:sqlite> select * from system;select * from system;<TR><TD>1</TD><TD>volume_music</TD><TD>7</TD></TR><TR><TD>4</TD><TD>volume_voice</TD><TD>4</TD></TR>........省略.........sqlite>".output"命令可以把查詢返回結果的輸出定向到檔案上。該命令的第一個參數即是要定向的位置。在把輸出定向了檔案後,可以通過".output stdout"把輸出重新定向到標準輸出上。樣本13:sqlite> .mode listsqlite> .separator |sqlite> .output test_file_1.txtsqlite> select * from tbl1;sqlite> .exit$ cat test_file_1.txthello|10goodbye|20$