SQLite3資料庫操作語法

2021-08-26 07:04:32 字數 3348 閱讀 6300

sql的指令格式

所以的sql指令都是以分號(;)結尾的。如果遇到兩個減號(--)則代表註解,sqlite3會略過去。

建立資料表

假設我們要建乙個名叫film的資料表,只要鍵入以下指令就可以了:

create table film(title, length, year, starring);

這樣我們就建立了乙個名叫film的資料表,裡面有name、length、year、starring四個字段。

這個create table指令的語法為:

create table table_name(field1, field2, field3, ...);

table_name是資料表的名稱,fieldx則是字段的名字。sqlite3與許多sql資料庫軟體不同的是,它不在乎字段屬於哪一種資料型態:sqlite3的字段可以儲存任何東西:文字、數字、大量文字(blub),它會在適時自動轉換。

建立索引

如果資料表有相當多的資料,我們便會建立索引來加快速度。好比說:

create index film_title_index on film(title);

意思是針對film資料表的name欄位,建立乙個名叫film_name_index的索引。這個指令的語法為

create index index_name on table_name(field_to_be_indexed);

一旦建立了索引,sqlite3會在針對該字段作查詢時,自動使用該索引。這一切的操作都是在幕後自動發生的,無須使用者特別指令。

加入一筆資料

接下來我們要加入資料了,加入的方法為使用insert into指令,語法為:

insert into table_name values(data1, data2, data3, ...);

例如我們可以加入

insert into film values ('silence of the lambs, the', 118, 1991, 'jodie foster');

insert into film values ('contact', 153, 1997, 'jodie foster');

insert into film values ('crouching tiger, hidden dragon', 120, 2000, 'yun-fat chow');

insert into film values ('hours, the', 114, 2002, 'nicole kidman');

如果該欄位沒有資料,我們可以填null。

查詢資料

講到這裡,我們終於要開始介紹sql最強大的select指令了。我們首先簡單介紹select的基本句型:

select columns from table_name where expression;

最常見的用法,當然是倒出所有資料庫的內容:

select * from film;

如果資料太多了,我們或許會想限制筆數:

select * from film limit 10;

或是照著電影年份來排列:

select * from film order by year limit 10;

或是年份比較近的電影先列出來:

select * from film order by year desc limit 10;

或是我們只想看電影名稱跟年份:

select title, year from film order by year desc limit 10;

查所有茱蒂佛斯特演過的電影:

select * from film where starring='jodie foster';

查所有演員名字開頭叫茱蒂的電影('%' 符號便是 sql 的萬用字元):

select * from film where starring like 'jodie%';

查所有演員名字以茱蒂開頭、年份晚於2023年、年份晚的優先列出、最多十筆,只列出電影名稱和年份:

select title, year from film where starring like 'jodie%' and year >= 1985 order by year desc limit 10;

有時候我們只想知道資料庫一共有多少筆資料:

select count(*) from film;

有時候我們只想知道2023年以後的電影有幾部:

select count(*) from film where year >= 1985;

(進一步的各種組合,要去看sql專書,不過你大概已經知道sql為什麼這麼流行了:這種語言允許你將各種查詢條件組合在一起──而我們還沒提到「跨資料庫的聯合查詢」呢!)

如何更改或刪除資料

了解select的用法非常重要,因為要在sqlite更改或刪除一筆資料,也是靠同樣的語法。

例如有一筆資料的名字打錯了:

update film set starring='jodie foster' where starring='jodee foster';

就會把主角欄位裡,被打成'jodee foster'的那筆(或多筆)資料,改回成jodie foster。

delete from film where year < 1970;

就會刪除所有年代早於2023年(不含)的電影了。

將結果寫到檔案

預設情況下,sqlite3會將結果傳送到標準輸出,你可以使用 ".output" 來改變,只是將輸出到的檔名作為引數傳遞給 .output,所有後面的查詢結果都會寫到檔案裡。開頭使用 ".output stdout" 會再次寫到標準輸出,例如:

sqlite> .mode list

sqlite> .separator |

sqlite> .output test_file_1.txt

sqlite> select * from tb1;

sqlite> .exit

# cat test_file_1.txt

hello|10

goodbye|20

其他sqlite的特別用法

sqlite可以在shell底下直接執行命令:

sqlite3 film.db "select * from film;"

輸出 html **:

sqlite3 -html film.db "select * from film;"

將資料庫「倒出來」:

sqlite3 film.db ".dump" > output.sql

利用輸出的資料,建立乙個一模一樣的資料庫(加上以上指令,就是標準的sql資料庫備份了):

sqlite3 film.db < output.sql

在大量插入資料時,你可能會需要先打這個指令:

begin;

sqlite3資料庫操作

1 開啟資料庫 1 需要制定資料庫的路徑 nsstring filepath nshomedirectory documents data.sqlite 2 建立資料庫的物件 sqlite3 qingyundb null 3 開啟命令 sqlite3 open dbfilepath utf8stri...

SQLite3資料庫操作

簡單的sqlite3語句,通過字串拼接執行資料庫操作。1.建立資料庫格式 db.execsql create table if not exists sharp id integer primary key,name varchar,level integer,high integer 其真正的有效...

Sqlite3 資料庫操作

一 sqlite3長用於 輕量級的 資料儲存,象微控制器這一類,但是現在的sqlite3,已經很先進,不能小看 二 sqlite3 常用命令 當前目錄下建立或開啟test.db 資料庫檔案,並進入 sqlite 命令終端,以 sqlite 字首標識 sqlite3 test.db 檢視資料庫檔案資訊...