《linux下sqlite3的特殊命令》要點:
本文介紹了linux下sqlite3的特殊命令,希望對您有用。如果有疑問,可以聯系我們。
相關主題:sqlite3
大多數候,sqlite3讀入輸入行,并把它們傳遞到SQLite庫中去運行。但是如果輸入行以一個點(“.”)開始,那么這行將被sqlite3程序自己截取并解釋。這些“點命令”通常被用來改變查詢輸出的格式,或者執行鞭個預封包(預定義prepackaged)的查詢語句。
你可以在任何時候輸入“.help”,列出可用的點命令。例如
sqlite> .help
.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
.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.
.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 on TABLE
.load FILE ?ENTRY? Load an extension library
.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
.schema ?TABLE? Show the CREATE statements
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.tables ?PATTERN? List names of tables matching a LIKE pattern
.timeout MS Try opening locked tables for MS milliseconds
.width NUM NUM ... Set column widths for "column" mode
sqlite>
改變輸出格式
sqlite3程序可以以八種不同的格式顯示一個查詢的結果:"csv", "列", "html", "插入", "行", "制表"和"tcl"。你可以用".mode"點命令在這些輸出格式之間切換。
默認的輸出格式是“列表”。在列表模式下,每條查詢結果記錄被寫在一行中并且每列之間以一個字符串分割符隔開。默認的分隔符是一個管道符號(“|”)。列表符號在當你輸出查詢結果到另外一個符加處理的程序(如AWK)中去是尤為有用。
sqlite> .mode list
sqlite> select * from tbl1;
hello|10
goodbye|20
sqlite>
你可以用“.separator”點命令來改變分界符。例如,為了把分割符改為一個逗號和一個空格,你可以這樣做:
sqlite> .separator ", "
sqlite> select * from tbl1;
hello, 10
goodbye, 20
sqlite>
在“line"模式下,每一個位于條記錄中的列在它自己那行顯示。每行由列名、一個等號和列數據組成。下一條記錄以一個空行隔開。這是一個行模式輸出的例子:
sqlite> .mode line
sqlite> select * from tbl1;
one = hello
two = 10
one = goodbye
two = 20
sqlite>
在列模式下,每條記錄在一個單獨的行中以數據列對齊的方式顯示。列如:
sqlite> .mode column
sqlite> select * from tbl1;
one two
---------- ----------
hello 10
goodbye 20
sqlite>
在默認的情況下,每列至少10個字符寬。太寬的數據將被截取。你可以用“.width”命令來調整列寬。如下所示:
sqlite> .width 12 6
sqlite> select * from tbl1;
one two
------------ ------
hello 10
goodbye 20
sqlite>
上面例子中".width"命令設置第一列寬為12第二列寬為6。其它的列寬不變。你可以指定與你查詢結果需要的列數一樣多的“.width”參數。
如果你指定一列寬為0,那么這個列寬將自動以下面三個數字中的最大值做為列寬:10、表頭寬度和最寬的數據列的寬度。這可以讓列自動調整寬度。每列的默認設置為自動調整的0值。
出現在輸出開頭兩行的列標示可以用".header"點命令關閉。在上面的例子中,列標示是打開的。可以用下面的方法關閉列標示:
sqlite> .header off
sqlite> select * from tbl1;
hello 10
goodbye 20
sqlite>
另外一個有用的輸出模式是"insert"。在插入模式下,被子格式化為看起來像SQL INSERT語句的樣式。你可以用插入模式來產生文件(便于)以后用于不同數據庫的輸入。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/86.html