iPhone應(yīng)用開發(fā) SQLite數(shù)據(jù)庫使用詳解
iPhone應(yīng)用開發(fā) SQLite數(shù)據(jù)庫使用詳解是本文要介紹對內(nèi)容,看到一篇好文章,與大家分享本篇文章。我們來看詳細(xì)內(nèi)容。
由于我主要負(fù)責(zé)我們小組項(xiàng)目數(shù)據(jù)庫模塊的部分所以這幾天都一直在研究在iphone中最為常用的一個(gè)簡單數(shù)據(jù)庫sqlite,自己也搜集很多資料,因此在這里總結(jié)一下這幾天的學(xué)習(xí)成果:
sqlite操作簡明教程
SQLite顧名思議是以 SQL為基礎(chǔ)的數(shù)據(jù)庫軟件,SQL是一套強(qiáng)大的數(shù)據(jù)庫語言,主要概念是由「數(shù)據(jù)庫」、「資料表」(table)、「查詢指令」(queries)等單元組成的「關(guān)聯(lián)性數(shù)據(jù)庫」(進(jìn)一步的概念可參考網(wǎng)絡(luò)上各種關(guān)于SQL及關(guān)聯(lián)性數(shù)據(jù)庫的文件)。因?yàn)镾QL的查詢功能強(qiáng)大,語法一致而入門容易,因此成為現(xiàn)今主流數(shù)據(jù)庫的標(biāo)準(zhǔn)語言(微軟、Oracle等大廠的數(shù)據(jù)庫軟件都提供SQL語法的查詢及操作)。
以下我們就建立數(shù)據(jù)庫、建立資料表及索引、新增資料、查詢資料、更改資料、移除資料、sqlite3命令列選項(xiàng)等幾個(gè)項(xiàng)目做簡單的介紹。
建立數(shù)據(jù)庫檔案
用sqlite3建立數(shù)據(jù)庫的方法很簡單,只要在shell下鍵入(以下$符號為shell提示號,請勿鍵入):
Sql代碼
- $ sqlite3 foo.db
- $ sqlite3 foo.db
如果目錄下沒有foo.db,sqlite3就會(huì)建立這個(gè)數(shù)據(jù)庫。sqlite3并沒有強(qiáng)制數(shù)據(jù)庫檔名要怎么取,因此如果你喜歡,也可以取個(gè)例如foo.icannameitwhateverilike的檔名。
在sqlite3提示列下操作
進(jìn)入了sqlite3之后,會(huì)看到以下文字:
- SQLite version 3.1.3Enter ".help" for instructionssqlite>
這時(shí)如果使用.help可以取得求助,.quit則是離開(請注意:不是quit)
SQL的指令格式
所以的SQL指令都是以分號(;)結(jié)尾的。如果遇到兩個(gè)減號(--)則代表注解,sqlite3會(huì)略過去。
建立資料表
假設(shè)我們要建一個(gè)名叫film的資料表,只要鍵入以下指令就可以了:
Sql代碼
- create table film(title, length, year, starring);
- create table film(title, length, year, starring);
這樣我們就建立了一個(gè)名叫film的資料表,里面有name、length、year、starring四個(gè)字段。
這個(gè)create table指令的語法為:
Sql代碼
- create table table_name(field1, field2, field3, ...);
- create table table_name(field1, field2, field3, ...);
table_name是資料表的名稱,fieldx則是字段的名字。sqlite3與許多SQL數(shù)據(jù)庫軟件不同的是,它不在乎字段屬于哪一種資料型態(tài):sqlite3的字段可以儲(chǔ)存任何東西:文字、數(shù)字、大量文字(blub),它會(huì)在適時(shí)自動(dòng)轉(zhuǎn)換。
建立索引
如果資料表有相當(dāng)多的資料,我們便會(huì)建立索引來加快速度。好比說:
Sql代碼
- create index film_title_index on film(title);
- create index film_title_index on film(title);
意思是針對film資料表的name字段,建立一個(gè)名叫film_name_index的索引。這個(gè)指令的語法為
Sql代碼
- create index index_name on table_name(field_to_be_indexed);
- create index index_name on table_name(field_to_be_indexed);
一旦建立了索引,sqlite3會(huì)在針對該字段作查詢時(shí),自動(dòng)使用該索引。這一切的操作都是在幕后自動(dòng)發(fā)生的,無須使用者特別指令。
加入一筆資料
接下來我們要加入資料了,加入的方法為使用insert into指令,語法為:
Sql代碼
- insert into table_name values(data1, data2, data3, ...);
- insert into table_name values(data1, data2, data3, ...);
例如我們可以加入
Sql代碼
- 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');
- 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的基本句型:
Sql代碼
- select columns from table_name where expression;
- select columns from table_name where expression;
最常見的用法,當(dāng)然是倒出所有數(shù)據(jù)庫的內(nèi)容:
Sql代碼
- select * from film;
- select * from film;
如果資料太多了,我們或許會(huì)想限制筆數(shù):
Sql代碼
- select * from film limit 10;
- select * from film limit 10;
或是照著電影年份來排列:
Sql代碼
- select * from film order by year limit 10;
- select * from film order by year limit 10;
或是年份比較近的電影先列出來:
Sql代碼
- select * from film order by year desc limit 10;
- select * from film order by year desc limit 10;
或是我們只想看電影名稱跟年份:
Sql代碼
- select title, year from film order by year desc limit 10;
- select title, year from film order by year desc limit 10;
查所有茱蒂佛斯特演過的電影:
Sql代碼
- select * from film where starring='Jodie Foster';
- select * from film where starring='Jodie Foster';
查所有演員名字開頭叫茱蒂的電影('%' 符號便是 SQL 的萬用字符):
Sql代碼
- select * from film where starring like 'Jodie%';
- select * from film where starring like 'Jodie%';
查所有演員名字以茱蒂開頭、年份晚于1985年、年份晚的優(yōu)先列出、最多十筆,只列出電影名稱和年份:
Sql代碼
- select title, year from film where starring like 'Jodie%' and year >= 1985 order by year desc limit 10;
- select title, year from film where starring like 'Jodie%' and year >= 1985 order by year desc limit 10;
有時(shí)候我們只想知道數(shù)據(jù)庫一共有多少筆資料:
Sql代碼
- select count(*) from film;
- select count(*) from film;
有時(shí)候我們只想知道1985年以后的電影有幾部:
Sql代碼
- select count(*) from film where year >= 1985;
- select count(*) from film where year >= 1985;
(進(jìn)一步的各種組合,要去看SQL專書,不過你大概已經(jīng)知道SQL為什么這么流行了:這種語言允許你將各種查詢條件組合在一起──而我們還沒提到「跨數(shù)據(jù)庫的聯(lián)合查詢」呢?。?/p>
如何更改或刪除資料
了解select的用法非常重要,因?yàn)橐趕qlite更改或刪除一筆資料,也是靠同樣的語法。
例如有一筆資料的名字打錯(cuò)了:
Sql代碼
- update film set starring='Jodie Foster' where starring='Jodee Foster';
- update film set starring='Jodie Foster' where starring='Jodee Foster';
就會(huì)把主角字段里,被打成'Jodee Foster'的那筆(或多筆)資料,改回成Jodie Foster。
Sql代碼
- delete from film where year < 1970;
- delete from film where year < 1970;
就會(huì)刪除所有年代早于1970年(不含)的電影了。
其他sqlite的特別用法
sqlite可以在shell底下直接執(zhí)行命令:
Sql代碼
- sqlite3 film.db "select * from film;"
- sqlite3 film.db "select * from film;"
輸出 HTML 表格:
Sql代碼
- sqlite3 -html film.db "select * from film;"
- sqlite3 -html film.db "select * from film;"
將數(shù)據(jù)庫「倒出來」:
Sql代碼
- sqlite3 film.db ".dump" > output.sql
- sqlite3 film.db ".dump" > output.sql
利用輸出的資料,建立一個(gè)一模一樣的數(shù)據(jù)庫(加上以上指令,就是標(biāo)準(zhǔn)的SQL數(shù)據(jù)庫備份了):
Sql代碼
- sqlite3 film.db < output.sql
- sqlite3 film.db < output.sql
在大量插入資料時(shí),你可能會(huì)需要先打這個(gè)指令:
begin;
插入完資料后要記得打這個(gè)指令,資料才會(huì)寫進(jìn)數(shù)據(jù)庫中:
commit;
以上我們介紹了SQLite這套數(shù)據(jù)庫系統(tǒng)的用法。事實(shí)上OS X也有諸于SQLiteManagerX這類的圖形接口程序,可以便利數(shù)據(jù)庫的操作。不過萬變不離其宗,了解SQL指令操作,SQLite與其各家變種就很容易上手了。
至于為什么要寫這篇教學(xué)呢?除了因?yàn)镺S X Tiger大量使用SQLite之外(例如:Safari的RSS reader,就是把文章存在SQLite數(shù)據(jù)庫里!你可以開開看~/Library/Syndication/Database3這個(gè)檔案,看看里面有什么料),OpenVanilla從0.7.2開始,也引進(jìn)了以SQLite為基礎(chǔ)的詞匯管理工具,以及全字庫的注音輸入法。因?yàn)槭褂肧QLite,這兩個(gè)模塊不管數(shù)據(jù)庫內(nèi)有多少筆資料,都可以做到「瞬間啟動(dòng)」以及相當(dāng)快速的查詢回應(yīng)。
將一套方便好用的數(shù)據(jù)庫軟件包進(jìn)OS X中,當(dāng)然也算是Apple相當(dāng)相當(dāng)聰明的選擇。再勤勞一點(diǎn)的朋友也許已經(jīng)開始想拿SQLite來記錄各種東西(像我們其中就有一人寫了個(gè)程序,自動(dòng)記錄電池狀態(tài),寫進(jìn)SQLite數(shù)據(jù)庫中再做統(tǒng)計(jì)......)了。想像空間可說相當(dāng)寬廣。
目前支援SQLite的程序語言,你能想到的大概都有了。這套數(shù)據(jù)庫2005年還贏得了美國O'Reilly Open Source Conference的***開放源代碼軟件獎(jiǎng),獎(jiǎng)評是「有什么東西能讓Perl, Python, PHP, Ruby語言團(tuán)結(jié)一致地支援的?就是SQLite」。由此可見SQLite的地位了。而SQLite程序非常小,更是少數(shù)打 "gcc -o sqlite3 *",不需任何特殊設(shè)定就能跨平臺(tái)編譯的程序。小而省,小而美,SQLite連網(wǎng)站都不多贅言,直指SQL語法精要及API使用方法,原作者大概也可以算是某種程序設(shè)計(jì)之道(Tao of Programming)里所說的至人了。
小結(jié):iPhone應(yīng)用開發(fā) SQLite數(shù)據(jù)庫使用詳解的內(nèi)容介紹完了,希望本文對你有所幫助!