在 iOS 開發裡,資料要怎麼落地一直是繞不開的話題。本文以資料庫為主線,先快速盤點幾種常見的持久化方案,再把 SQLite 的 SQL 語法系統性地梳理一遍。
其中 Plist、Preference、NSCoding 這三種屬於「文件存儲」的範疇,可以參考 iOS 檔案存儲方式的相關說明,這裡不再展開。
Core Data 提供了 ORM(物件-關聯映射)能力,能把 Objective-C 物件轉換成資料寫入 SQLite 檔案,也能把資料庫中的記錄還原成物件,整個過程中完全不需要手寫 SQL。功能強大的代價是效能損耗也相對明顯。若你更在意可控性與執行效率,直接操作 SQLite(或搭配 FMDB)往往是更合適的選擇。
SQLite是一款嵌入式資料庫,佔用資源極低,在嵌入式設備上常常幾百 K 記憶體就足夠,處理速度甚至比 MySQL、PostgreSQL 這類大型資料庫還快。
資料庫本質上是依資料結構來組織、存儲與管理資料的倉庫,可分為關聯式資料庫(主流)與物件型資料庫兩大類。常見的關聯式資料庫有 SQLite、MySQL、Oracle。
資料庫的存儲結構和 Excel 很像,以「表(table)」為單位。存資料的步驟是:先建表,再定義多個欄位(column/列/屬性),最後寫入多筆記錄(row),每一列存放各欄位對應的值。
SQL 有幾個值得先記住的特點:不區分大小寫;每條語句必須以分號結尾;資料庫中不允許使用關鍵字來命名表或欄位。常用關鍵字包括 select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index 等。
建表語法如下,實務上表名通常加上 t_ 前綴:
create table 表名 (欄位名1 欄位型別1, 欄位名2 欄位型別2, ...);
create table if not exists 表名 (欄位名1 欄位型別1, ...);
常見欄位型別有四種:integer(整數)、real(浮點數)、text(文字字串)、blob(二進位資料,例如檔案)。
create table t_student (id integer, name text, age integer, score real);
刪表則用 drop:
drop table 表名;
drop table if exists 表名;
drop table t_student;
插入時要注意,字串內容必須用單引號包住:
insert into 表名 (欄位1, 欄位2, ...) values (欄位1的值, 欄位2的值, ...);
insert into t_student (name, age) values ('kingsly', 20);
更新語法如下。以下範例會把 t_student 表中所有記錄的 name 改為 Roger、age 改為 34:
update 表名 set 欄位1 = 值1, 欄位2 = 值2 ...;
update t_student set name = 'Roger', age = 34;
刪除語法如下。以下範例會清空 t_student 表中的所有記錄:
delete from 表名;
delete from t_student;
select 欄位1, 欄位2, ... from 表名;
select * from 表名; // 查詢所有欄位
select name, age from t_student;
select * from t_student;
查詢時可以為欄位或表取別名,寫 `as` 或省略 `as` 都可以:
select 欄位1 別名, 欄位2 as 別名 from 表名 as 別名;
select name myname, age myage from t_student;
select s.name, s.age from t_student s;
統計筆數則使用 count:
select count(欄位) from 表名;
select count(*) from 表名;
select count(age) from t_student;
如果只想更新或刪除部分記錄,就必須在 DML 語句後面加上條件。注意 SQL 中的等號只能寫一個:
where 欄位 = 某個值;
where 欄位 != 某個值;
where 欄位 > 某個值;
where 欄位1 = 值1 and 欄位2 > 值2; // and 等同 C 語言的 &&
where 欄位1 = 值1 or 欄位2 = 值2; // or 等同 C 語言的 ||
例如把年齡大於 10 且姓名不是 jack 的記錄,年齡都改成 5:
update t_student set age = 5 where age > 10 and name != 'jack';
查詢結果可以用 order by 排序,預設是升序,加上 desc 則變為降序,也支援多欄位排序:
select * from t_student order by age;
select * from t_student order by age desc;
select * from t_student order by age asc, height desc;
需要控制返回筆數時使用 limit,語法為 `limit 數值1, 數值2`,代表跳過前 N 筆之後再取 M 筆:
select * from t_student limit 5, 10; // 從第 5 筆之後取 10 筆
建表時可以為特定欄位加上約束條件,常見的有三種:
create table t_student (id integer, name text not null unique, age integer not null default 1);
主鍵用來唯一標識某一筆記錄。如果 t_student 表沒有主鍵,就可能出現兩筆 name 和 age 完全相同的記錄而無法區分,這時候就需要類似身分證字號這樣的標識。主鍵可以是一個或多個欄位,理想情況下應由程式自動產生,不 interference 使用者的資料。
create table t_student (id integer primary key, name text, age integer);
create table t_student (id integer primary key autoincrement, name text, age integer);
主鍵欄位預設就已包含 not null 與 unique 兩個約束;加上 autoincrement 之後,integer 類型的主鍵會自動遞增,管理起來更省事。
外鍵用來建立表與表之間的關聯,也就是某個表中的一個欄位,其值來自另一張表的某個欄位。例如學生表的 class_id 來自班級表的 id:
create table t_student (
id integer primary key autoincrement,
name text,
age integer,
class_id integer,
constraint fk_t_student_class_id_t_class_id
foreign key (class_id) references t_class (id)
);
需要跨多張表才能取得目標資料時,就要用到表連接。常見類型有兩種:內連接(inner join 或 join),只顯示左右兩表欄位值都完整的記錄;左外連接(left outer join),保證左表資料的完整性。
select s.name, s.age
from t_student s, t_class c
where s.class_id = c.id and c.name = '網絡工程2班';