android sqlite3命令

來源:互聯網
上載者:User

sqlite3: 一個SQLite資料庫的命令列介面 

        原文地址:http://www.sqlite.org/sqlite.html 

    SQLite庫包含一個名字叫做sqlite3的命令列,它可以讓使用者手工輸入並執行面向SQLite資料庫的SQL命令。本文檔提供一個樣使用sqlite3的簡要說明。 

開始 

      啟動sqlite3程式,僅僅需要敲入帶有SQLite資料庫名字的"sqlite3"命令即可。如果檔案不存在,則建立一個新的(資料庫)檔案。然後 sqlite3程式將提示你輸入SQL。敲入SQL語句(以分號“;”結束),敲斷行符號鍵之後,SQL語句就會執行。 

        例如,建立一個包含一個表"tb11"名字為"ex1"的SQLite資料庫,你可以這樣做: 

$sqlite3 ex1 
SQLite version 3.3.17 
Enter ".help" for instructions 
sqlite> create table tbl1(one varchar(10), two smallint); 
sqlite> insert into tbl1 values('hello!', 10); 
sqlite> insert into tbl1 values('goodbye', 20); 
sqlite> select * from tbl1; 
hello!|10 
goodbye|20 
sqlite> 

        你可以通過敲你所用系統的檔案結束符(通常是Ctrl + D)或者中斷字元(通常是Ctrl + C)。來終止sqlite3程式。確定你在每個SQL語句結束敲入分號!sqlite3程式通過尋找分號來決定一個SQL語句的結束。如果你省略分號,sqlite3將給你一個連續的命令提示字元並等你給當前的SQL命令添加更多的文字。這個特點讓你輸入多行的多個SQL語句,例如: 

sqlite> create table tbl2( 
   ...> f1 varchar(30) primary key, 
   ...> f2 text, 
   ...> f3 real 
   ...> ); 
sqlite> 

題外話:查詢SQLITE_MASTER表 

    SQLite資料庫的架構被儲存在一個名叫"sqlite_master"的特殊的表中。你可以像查詢其它表一樣通過執行“SELECT”查詢這個特殊的表。例如:

$ sqlite3 ex1 
SQlite vresion 3.3.10 
Enter ".help" for instructions 
sqlite> select * from sqlite_master; 
    type = table 
    name = tbl1 
tbl_name = tbl1 
rootpage = 3 
     sql = create table tbl1(one varchar(10), two smallint) 
sqlite> 

    但你不能在sqlite_master表中執行諸如DROP TABLE, UPDATE, INSERT 或者DELETE命令。sqlite_master表在你建立、刪除和索引資料庫時自動更新這個表。你不能手工更改sqlite_master表。 

    TEMPORARY表的結構沒有儲存在"sqlite_master"表中,由於TEMPORARY表對應用是不可見的,而不是應用程式建立這個表。 TEMPORARY表結構被儲存在另外一個名叫"sqlite_temp_master"的特定的表中。"sqlite_temp_master"表是暫存資料表自身。 

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語句的樣式。你可以用插入模式來產生檔案(便於)以後用於不同資料庫的輸入。 

    當指定插入模式時,你必須給定一個特定參數就是要插入的表名。例如: 

sqlite> .mode insert new_table 
sqlite> select * from tbl1; 
INSERT INTO 'new_table' VALUES('hello',10); 
INSERT INTO 'new_table' VALUES('goodbye',20); 
sqlite> 

    最新的輸出格式是“html”。在這種模式下,sqlite3把查詢的結果寫做XHTML表。開始的<TABLE>和結束的</TABLE>(標記)沒有寫出,但有<TR>、<TH>和<TD>等分界符。html輸出對 CGI來說是相當有用地。 

把結果寫到檔案中 

    預設情況下,sqlte3把結送到標準輸出。你可以用“.output”命令改變它。只須把輸出檔案名做為.output命令的輸出參數然後所有後續查詢結果將被寫到那個檔案中。用“.output stdout”再一次改為標準輸出。例如: 

sqlite> .mode list 
sqlite> .separator | 
sqlite> .output test_file_1.txt 
sqlite> select * from tbl1; 
sqlite> .exit 
$ cat test_file_1.txt 
hello|10 
goodbye|20 

查詢資料庫結構 

    sqlite3程式提供幾個有用的用於查詢資料庫結構的快捷命令。這些不是不可以用別的方式來實現。這些命令僅僅是一個捷徑而已。 

    例如,為了查看資料庫的表列表,你可以敲入“.tables”。 

sqlite> .tables 
tbl1 
tbl2 
sqlite> 

    “.tables”命令相似於設定列表模式然後執行接下來的查詢: 

SELECT name FROM sqlite_master 
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' 
UNION ALL 
SELECT name FROM sqlite_temp_master 
WHERE type IN ('table','view') 
ORDER BY 1事實上,你可以查看sqlite3的原始碼(可以在源檔案樹的src/shell.c中),你可找到上面的具體的查詢。“.indices”命令作用類似的方式是列出特定表的所有的索引。“.indics”命令須一個參數即所要索引表的表名。最後,但不是至少,是“.schema”命令。不帶任何參數,“.schema”命令顯示原始的用於建立當前資料庫的CREATE TABLE和CREATE INDEX語句。如果你給".schema"命令一個表名,它顯示原始的建立該表和它所有索引的CREATE語句。我們可以:sqlite>
.schemacreate table tbl1(one varchar(10), two smallint)CREATE TABLE tbl2 ( f1 varchar(30) primary key, f2 text, f3 real)sqlite> .schema tbl2CREATE TABLE tbl2 ( f1 varchar(30) primary key, f2 text, f3 real)sqlite>    ".schema"命令可以用設定列表然後執行以下查詢來實現: 

SELECT sql FROM 
   (SELECT * FROM sqlite_master UNION ALL 
    SELECT * FROM sqlite_temp_master) 
WHERE type!='meta' 
ORDER BY tbl_name, type DESC, name 

.databases 列出資料庫檔案名 
.tables ?PATTERN? 列出?PATTERN?匹配的表名 
.import FILE TABLE 將檔案中的資料匯入的檔案中 
.dump ?TABLE? 產生形成資料庫表的SQL指令碼 
.output FILENAME 將輸出匯入到指定的檔案中 
.output stdout 將輸出列印到螢幕 
.mode MODE ?TABLE?     設定資料輸出模式(csv,html,tcl… 
.nullvalue STRING 用指定的串代替輸出的NULL串 
.read FILENAME 執行指定檔案中的SQL語句 
.schema ?TABLE? 列印建立資料庫表的SQL語句 
.separator STRING 用指定的字串代替欄位分隔符號 
.show 列印所有SQLite環境變數的設定 
.quit 退出命令列介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.