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>