標籤:
- Plist(NSArry\NSDictionary)
- Preference(喜好設定\NSUserDefaults
- NSCoding(NSKeyedArchiver\NSkeyedUnarchiver)
- SQLite3
- Core Data
SQLite3
-
什麼是SQLite
SQLite是一款輕型的嵌入式資料庫
它佔用資源非常的低,在嵌入式裝置中,可能只需要幾百K的記憶體就夠了
它的處理速度比Mysql、PostgreSQL這兩款著名的資料庫都還快
-
什麼是資料庫
資料庫(Database)是按照資料結構來組織、儲存和管理資料的倉庫
資料庫可以分為2大種類
關係型資料庫(主流)
對象型資料庫
-
常用關係型資料庫
PC端:Oracle、MySQL、SQL Server、Access、DB2、Sybase
嵌入式\移動用戶端:SQLite
如何儲存資料
-
資料庫是如何儲存資料的
- 資料庫的儲存結構和excel很像,以表(table)為單位
-
資料庫儲存資料的步驟
- 建立資料庫檔案
- 建立一張表(table)
- 添加多個欄位(column,列,屬性)
- 添加多行記錄(row,每行存放多個欄位對應的值)
Navicat
- Navicat是一款著名的資料庫管理軟體,支援大部分主流資料庫(包括SQLite
- 利用Navicat建立資料庫連接 ?
建表
?
?
command+s 儲存填寫表的名稱
?
查看DDl
?
執行SQL語句
?
SQL語句
select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等
SQL語句的種類
-
資料定義語句(DDL:Data Definition Language)
- 包括create和drop等操作
- 在資料庫中建立新表或刪除表(create table或 drop table)
-
資料動作陳述式(DML:Data Manipulation Language)
- 包括insert、update、delete等操作
- 上面的3種操作分別用於添加、修改、刪除表中的資料
-
資料查詢語句(DQL:Data Query Language)
- 可以用於查詢獲得表中的資料
- 關鍵字select是DQL(也是所有SQL)用得最多的操作
- 其他DQL常用的關鍵字有where,order by,group by和having
欄位類型
integer : 整型值real : 浮點值text : 文本字串blob : 位元據(比如檔案)
創表
-
格式
create table 表名 (欄位名1 欄位類型1, 欄位名2 欄位類型2, …) ;create table if not exists 表名 (欄位名1 欄位類型1, 欄位名2 欄位類型2, …) ;
-
樣本
create table t_student (id integer, name text, age inetger, score real) ;
刪除表
drop table 表名 ;drop table if exists 表名 ;
- 樣本
drop table t_student ;
插入資料(insert)
insert into 表名 (欄位1, 欄位2, …) values (欄位1的值, 欄位2的值, …) ;
insert into t_student (name, age) values (‘zhangsan’, 10) ;
更新資料(update)
update 表名 set 欄位1 = 欄位1的值, 欄位2 = 欄位2的值, … ;
update t_student set name = ‘jack’, age = 20 ;
- 注意
- 上面的樣本會將t_student表中所有記錄的name都改為jack,age都改為20
刪除資料(delete)
delete from 表名 ;
delete from t_student ;
- 注意
- 上面的樣本會將t_student表中所有記錄都刪掉
條件陳述式
where 欄位 = 某個值 ; // 不能用兩個 =where 欄位 is 某個值 ; // is 相當於 = where 欄位 != 某個值 ; where 欄位 is not 某個值 ; // is not 相當於 != where 欄位 > 某個值 ; where 欄位1 = 某個值 and 欄位2 > 某個值 ; // and相當於C語言中的 &&where 欄位1 = 某個值 or 欄位2 = 某個值 ; // or 相當於C語言中的 ||
條件陳述式練習
- 樣本
- 將t_student表中年齡大於10 並且 姓名不等於jack的記錄,年齡都改為 5
update t_student set age = 5 where age > 10 and name != ‘jack’ ;
- 刪除t_student表中年齡小於等於10 或者 年齡大於30的記錄
delete from t_student where age <= 10 or age > 30 ;
update t_student set score = age where name = ‘jack’ ;
- 將t_student表中名字等於jack的記錄,score欄位的值 都改為 age欄位的值
DQL語句
select 欄位1, 欄位2, … from 表名 ;select * from 表名; // 查詢所有的欄位
select name, age from t_student ;select * from t_student ;select * from t_student where age > 10 ; // 條件查詢
起別名
select 欄位1 別名 , 欄位2 別名 , … from 表名 別名 ; select 欄位1 別名, 欄位2 as 別名, … from 表名 as 別名 ;select 別名.欄位1, 別名.欄位2, … from 表名 別名 ;
select name myname, age myage from t_student ;
- 給name起個叫做myname的別名,給age起個叫做myage的別名
select s.name, s.age from t_student s ;
- 給t_student表起個別名叫做s,利用s來參考資料表中的欄位
計算記錄的數量
select count (欄位) from 表名 ;select count ( * ) from 表名 ;
select count (age) from t_student ;select count ( * ) from t_student where score >= 60;
排序
select * from t_student order by 欄位 ;select * from t_student order by age ;
預設是按照升序排序(由小到大),也可以變為降序(由大到小)
select * from t_student order by age desc ; //降序select * from t_student order by age asc ; // 升序(預設)
select * from t_student order by age asc, height desc ;
先按照年齡排序(升序),年齡相等就按照身高排序(降序)
limit
select * from 表名 limit 數值1, 數值2 ;
select * from t_student limit 4, 8 ;
可以理解為:跳過最前面4條語句,然後取8條記錄
limit常用來做分頁查詢,比如每頁固定顯示5條資料,那麼應該這樣取資料
第1頁:limit 0, 5
第2頁:limit 5, 5
第3頁:limit 10, 5
…
第n頁:limit 5*(n-1), 5
select * from t_student limit 7 ;相當於select * from t_student limit 0, 7 ;
表示取最前面的7條記錄
簡單約束
create table t_student (id integer, name text not null unique, age integer not null default 1) ;name欄位不能為null,並且唯一age欄位不能為null,並且預設為1
主鍵約束
主鍵設計的原則
-
主鍵應當是對使用者沒有意義的
-
永遠也不要更新主鍵
-
主鍵不應包含動態變化的資料
-
主鍵應當由電腦自動產生
主鍵的聲明
create table t_student (id integer primary key, name text, age integer) ;integer類型的id作為t_student表的主鍵
create table t_student (id integer primary key autoincrement, name text, age integer) ;
外鍵約束
-
利用外鍵約束可以用來建立表與表之間的聯絡
- 外鍵的一般情況是:一張表的某個欄位,引用著另一張表的主鍵欄位
-
建立一個外鍵
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)); t_student表中有一個叫做fk_t_student_class_id_t_class_id的外鍵
- 這個外鍵的作用是用t_student表中的class_id欄位引用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 = ‘6(1)’;
iOS中的資料的儲存方式