《linux下sqlite3的特殊命令》要點(diǎn):
本文介紹了linux下sqlite3的特殊命令,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:sqlite3
大多數(shù)候,sqlite3讀入輸入行,并把它們傳遞到SQLite庫(kù)中去運(yùn)行。但是如果輸入行以一個(gè)點(diǎn)(“.”)開(kāi)始,那么這行將被sqlite3程序自己截取并解釋。這些“點(diǎn)命令”通常被用來(lái)改變查詢輸出的格式,或者執(zhí)行鞭個(gè)預(yù)封包(預(yù)定義prepackaged)的查詢語(yǔ)句。
你可以在任何時(shí)候輸入“.help”,列出可用的點(diǎn)命令。例如
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程序可以以八種不同的格式顯示一個(gè)查詢的結(jié)果:"csv", "列", "html", "插入", "行", "制表"和"tcl"。你可以用".mode"點(diǎn)命令在這些輸出格式之間切換。
默認(rèn)的輸出格式是“列表”。在列表模式下,每條查詢結(jié)果記錄被寫(xiě)在一行中并且每列之間以一個(gè)字符串分割符隔開(kāi)。默認(rèn)的分隔符是一個(gè)管道符號(hào)(“|”)。列表符號(hào)在當(dāng)你輸出查詢結(jié)果到另外一個(gè)符加處理的程序(如AWK)中去是尤為有用。
sqlite> .mode list
sqlite> select * from tbl1;
hello|10
goodbye|20
sqlite>
你可以用“.separator”點(diǎn)命令來(lái)改變分界符。例如,為了把分割符改為一個(gè)逗號(hào)和一個(gè)空格,你可以這樣做:
sqlite> .separator ", "
sqlite> select * from tbl1;
hello, 10
goodbye, 20
sqlite>
在“l(fā)ine"模式下,每一個(gè)位于條記錄中的列在它自己那行顯示。每行由列名、一個(gè)等號(hào)和列數(shù)據(jù)組成。下一條記錄以一個(gè)空行隔開(kāi)。這是一個(gè)行模式輸出的例子:
sqlite> .mode line
sqlite> select * from tbl1;
one = hello
two = 10
one = goodbye
two = 20
sqlite>
在列模式下,每條記錄在一個(gè)單獨(dú)的行中以數(shù)據(jù)列對(duì)齊的方式顯示。列如:
sqlite> .mode column
sqlite> select * from tbl1;
one two
---------- ----------
hello 10
goodbye 20
sqlite>
在默認(rèn)的情況下,每列至少10個(gè)字符寬。太寬的數(shù)據(jù)將被截取。你可以用“.width”命令來(lái)調(diào)整列寬。如下所示:
sqlite> .width 12 6
sqlite> select * from tbl1;
one two
------------ ------
hello 10
goodbye 20
sqlite>
上面例子中".width"命令設(shè)置第一列寬為12第二列寬為6。其它的列寬不變。你可以指定與你查詢結(jié)果需要的列數(shù)一樣多的“.width”參數(shù)。
如果你指定一列寬為0,那么這個(gè)列寬將自動(dòng)以下面三個(gè)數(shù)字中的最大值做為列寬:10、表頭寬度和最寬的數(shù)據(jù)列的寬度。這可以讓列自動(dòng)調(diào)整寬度。每列的默認(rèn)設(shè)置為自動(dòng)調(diào)整的0值。
出現(xiàn)在輸出開(kāi)頭兩行的列標(biāo)示可以用".header"點(diǎn)命令關(guān)閉。在上面的例子中,列標(biāo)示是打開(kāi)的。可以用下面的方法關(guān)閉列標(biāo)示:
sqlite> .header off
sqlite> select * from tbl1;
hello 10
goodbye 20
sqlite>
另外一個(gè)有用的輸出模式是"insert"。在插入模式下,被子格式化為看起來(lái)像SQL INSERT語(yǔ)句的樣式。你可以用插入模式來(lái)產(chǎn)生文件(便于)以后用于不同數(shù)據(jù)庫(kù)的輸入。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/86.html