iOS開發(fā)數(shù)據(jù)庫篇—SQLite簡單介紹
一、離線緩存
在項目開發(fā)中天揖,通常都需要對數(shù)據(jù)進行離線緩存的處理夺欲,如新聞數(shù)據(jù)的離線緩存等。
說明:離線緩存一般都是把數(shù)據(jù)保存到項目的沙盒中今膊。有以下幾種方式
(1)歸檔:NSCodeing些阅、NSKeyedArchiver
(2)偏好設(shè)置:NSUserDefaults
(3)Plist存儲:writeToFile
提示:上述三種方法都有一個致命的缺點,那就是都無法存儲大批量的數(shù)據(jù)斑唬,有性能的問題市埋。
舉例:使用歸檔
兩個問題:
(1)數(shù)據(jù)的存取都必須是完整的,要求寫入的時候要一次性寫入恕刘,讀取的時候要一次性全部讀取缤谎,這涉及到應(yīng)用的性能問題。
(2)如果有1000條數(shù)據(jù)褐着,此時要把第1001條數(shù)據(jù)存入弓千,那么需要把所有的數(shù)據(jù)取出來,把這條數(shù)據(jù)加上去之后献起,再存入洋访。
說明:以上的三種技術(shù)不能處理大批量數(shù)據(jù)的存儲镣陕,大批量數(shù)據(jù)通常使用數(shù)據(jù)庫來進行存儲。
二姻政、SQLite簡單介紹
1.ios中數(shù)據(jù)的存儲方式
(1)Plist(NSArray\NSDictionary)
(2)Preference(偏好設(shè)置\NSUserDefaults)
(3)NSCoding(NSKeyedArchiver\NSkeyedUnarchiver)
(4)SQLite3
(5)Core Data
說明:
3是版本號呆抑,是SQLite的第三個版本。
core Data是對SQLite的封裝汁展,因為iOS中使用的SQLite是純C語言的鹊碍。
2.SQLite
(1)什么是SQLite?
答:SQLite是一款輕型的嵌入式數(shù)據(jù)庫食绿,安卓和ios開發(fā)使用的都是SQLite數(shù)據(jù)庫
(2)特點(優(yōu)點)
答:1)它占用資源非常的低侈咕,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了
2)它的處理速度比Mysql器紧、PostgreSQL這兩款著名的數(shù)據(jù)庫都還快
(3)什么是數(shù)據(jù)庫
答:數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織耀销、存儲和管理數(shù)據(jù)的倉庫
(4)數(shù)據(jù)庫的分類
答:可以分為2大種類
關(guān)系型數(shù)據(jù)庫(主流)和對象型數(shù)據(jù)庫(直接把內(nèi)存中的對象塞入到數(shù)據(jù)庫,對比關(guān)系型數(shù)據(jù)庫而言性能不能很好铲汪,效率不高)
(5)常用關(guān)系型數(shù)據(jù)庫有哪些熊尉?
答:PC端:Oracle、MySQL掌腰、SQL Server狰住、Access、DB2齿梁、Sybase
嵌入式\移動客戶端:SQLite
(6)數(shù)據(jù)庫是如何存儲數(shù)據(jù)的催植?
答:數(shù)據(jù)庫的存儲結(jié)構(gòu)和excel很像,以表(table)為單位 勺择。表由多個字段(列查邢、屬性、column)組成酵幕,表里面的每一行數(shù)據(jù)稱為記錄
(7)數(shù)據(jù)庫存儲數(shù)據(jù)的步驟?
1)新建一張表(table)
2)添加多個字段(column,列缓苛,屬性)
3)添加多行記錄(row芳撒,record,每行存放多個字段對應(yīng)的值)
iOS開發(fā)數(shù)據(jù)庫篇—SQL
一未桥、SQL語句
如果要在程序運行過程中操作數(shù)據(jù)庫中的數(shù)據(jù)笔刹,那得先學會使用SQL語句
1.什么是SQL
SQL(structured query language):結(jié)構(gòu)化查詢語言
SQL是一種對關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)進行定義和操作的語言
SQL語言簡潔,語法簡單冬耿,好學好用
2.什么是SQL語句
使用SQL語言編寫出來的句子\代碼舌菜,就是SQL語句
在程序運行過程中,要想操作(增刪改查亦镶,CRUD)數(shù)據(jù)庫中的數(shù)據(jù)日月,必須使用SQL語句
3.SQL語句的特點
不區(qū)分大小寫(比如數(shù)據(jù)庫認為user和UsEr是一樣的)
每條語句都必須以分號 ; 結(jié)尾
4.SQL中的常用關(guān)鍵字有
select袱瓮、insert、update爱咬、delete尺借、from、create精拟、where燎斩、desc、order蜂绎、by栅表、group、table师枣、alter怪瓶、view、index等等
數(shù)據(jù)庫中不可以使用關(guān)鍵字來命名表坛吁、字段
二劳殖、SQL語句的種類
1.數(shù)據(jù)定義語句(DDL:Data Definition Language)
包括create和drop等操作
在數(shù)據(jù)庫中創(chuàng)建新表或刪除表(create table或 drop table)
2.數(shù)據(jù)操作語句(DML:Data Manipulation Language)
包括insert、update拨脉、delete等操作
上面的3種操作分別用于添加哆姻、修改、刪除表中的數(shù)據(jù)
3.數(shù)據(jù)查詢語句(DQL:Data Query Language)
可以用于查詢獲得表中的數(shù)據(jù)
關(guān)鍵字select是DQL(也是所有SQL)用得最多的操作
其他DQL常用的關(guān)鍵字有where玫膀,order by矛缨,group by和having
三、基本操作
1.創(chuàng)建表
create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
示例
create table t_student (id integer, name text, age integer, score real) ;
2.字段類型
SQLite將數(shù)據(jù)劃分為以下幾種存儲類型:
integer : 整型值
real : 浮點值
text : 文本字符串
blob : 二進制數(shù)據(jù)(比如文件)
注意:實際上SQLite是無類型的帖旨,就算聲明為integer類型箕昭,還是能存儲字符串文本(主鍵除外)
建表時聲明啥類型或者不聲明類型都可以,也就意味著創(chuàng)表語句可以這么寫:
create table t_student(name, age);
提示:為了保持良好的編程規(guī)范解阅、方便程序員之間的交流落竹,編寫建表語句的時候最好加上每個字段的具體類型
3.刪表
格式
drop table 表名 ;
drop table if exists 表名 ;
示例
drop table t_student ;
4.插入數(shù)據(jù)(insert)
格式
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
示例
insert into t_student (name, age) values (‘mj’, 10) ;
注意
數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號 ’ 括住
5.更新數(shù)據(jù)(update)
格式
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
示例
update t_student set name = ‘jack’, age = 20 ;
注意
上面的示例會將t_student表中所有記錄的name都改為jack,age都改為20
6.刪除數(shù)據(jù)(delete)
格式
delete from 表名 ;
示例
delete from t_student ;
注意
上面的示例會將t_student表中所有記錄都刪掉
7.條件語句
如果只想更新或者刪除某些固定的記錄货抄,那就必須在DML語句后加上一些條件
條件語句的常見格式
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字段的值
8.DQL語句
格式
select 字段1, 字段2, … from 表名 ;
select * from 表名; // 查詢所有的字段
示例
select name, age from t_student ;
select * from t_student ;
select * from t_student where age > 10 ; // 條件查詢
9.起別名
格式(字段和表都可以起別名)
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來引用表中的字段
10.計算記錄的數(shù)量
格式
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
示例
select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;
11.排序
查詢出來的結(jié)果可以用order by進行排序
select * from t_student order by 字段 ;
select * from t_student order by age ;
默認是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫怪与。?/p>
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 ;
先按照年齡排序(升序)夺刑,年齡相等就按照身高排序(降序)
12.limit
使用limit可以精確地控制查詢結(jié)果的數(shù)量,比如每次只查詢10條數(shù)據(jù)
格式
select * from 表名 limit 數(shù)值1, 數(shù)值2 ;
示例
select * from t_student limit 4, 8 ;
可以理解為:跳過最前面4條語句,然后取8條記錄
limit常用來做分頁查詢遍愿,比如每頁固定顯示5條數(shù)據(jù)存淫,那么應(yīng)該這樣取數(shù)據(jù)
第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條記錄
四、約束
1.簡單約束
建表時可以給特定的字段設(shè)置一些約束條件错览,常見的約束有
not null :規(guī)定字段的值不能為null
unique :規(guī)定字段的值必須唯一
default :指定字段的默認值
(建議:盡量給字段設(shè)定嚴格的約束纫雁,以保證數(shù)據(jù)的規(guī)范性)
示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
name字段不能為null,并且唯一
age字段不能為null倾哺,并且默認為1
2.主鍵約束
(1)簡單說明
如果t_student表中就name和age兩個字段轧邪,而且有些記錄的name和age字段的值都一樣時,那么就沒法區(qū)分這些數(shù)據(jù)羞海,造成數(shù)據(jù)庫的記錄不唯一忌愚,這樣就不方便管理數(shù)據(jù)
良好的數(shù)據(jù)庫編程規(guī)范應(yīng)該要保證每條記錄的唯一性,為此却邓,增加了主鍵約束
也就是說硕糊,每張表都必須有一個主鍵,用來標識記錄的唯一性
(2)什么是主鍵腊徙?
主鍵(Primary Key简十,簡稱PK)用來唯一地標識某一條記錄
例如t_student可以增加一個id字段作為主鍵,相當于人的身份證
主鍵可以是一個字段或多個字段
(3)主鍵的設(shè)計原則
主鍵應(yīng)當是對用戶沒有意義的
永遠也不要更新主鍵
主鍵不應(yīng)包含動態(tài)變化的數(shù)據(jù)
主鍵應(yīng)當由計算機自動生成
(4)主鍵的聲明
在創(chuàng)表的時候用primary key聲明一個主鍵
create table t_student (id integer primary key, name text, age integer) ;
integer類型的id作為t_student表的主鍵
主鍵字段
只要聲明為primary key撬腾,就說明是一個主鍵字段
主鍵字段默認就包含了not null 和 unique 兩個約束
說明:如果想要讓主鍵自動增長(必須是integer類型)螟蝙,應(yīng)該增加autoincrement
create table t_student (id integer primary key autoincrement, name text, age integer) ;
3.外鍵約束
利用外鍵約束可以用來建立表與表之間的聯(lián)系
外鍵的一般情況是:一張表的某個字段,引用著另一張表的主鍵字段
新建一個外鍵
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_student_class 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字段
4.表連接查詢
表連接查詢:需要聯(lián)合多張表才能查到想要的數(shù)據(jù)
表連接的類型
內(nèi)連接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄)
左外連接:left outer join (保證左表數(shù)據(jù)的完整性)
示例
查詢0316iOS班的所有學生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS’;
數(shù)據(jù)庫升級
//判斷字段是否存在
if ([self.db columnExists:kScanCardPath inTableWithName:TABLE_CARD]) {
NSString *update = [NSString stringWithFormat:@"UPDATE %@ SET %@=? WHERE %@ = '%@'",TABLE_CARD,kScanCardPath,kLiuDuID,liuduid];
[self.db executeUpdate:update,path];
}else{
NSString *sql = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ text",TABLE_CARD,kScanCardPath];
[self.db executeUpdate:sql];
NSString *update = [NSString stringWithFormat:@"UPDATE %@ SET %@=? WHERE %@ = '%@'",TABLE_CARD,kScanCardPath,kLiuDuID,liuduid];
[self.db executeUpdate:update,path];
}