iOS中進(jìn)行數(shù)據(jù)持久化存儲(chǔ)有幾種方式
- plist(NSArray NSDictionary只用來(lái)存儲(chǔ)數(shù)組和字典,并且數(shù)組和字典中不能有自定義對(duì)象,有自定義對(duì)象)
- preference偏好設(shè)置也不能存儲(chǔ)自定義對(duì)象
- nscoding歸檔:用來(lái)存儲(chǔ)自定義對(duì)象.局限性:一次性做存儲(chǔ)/讀取操作.------------------而離線緩存是有時(shí)是一次一次增加,如果用歸檔,就需要,從數(shù)組中讀取出來(lái)加入數(shù)組,再重新存入.不能單獨(dú)添加某一個(gè)東西.必須一次性添加進(jìn)去.
比如有
80條數(shù)據(jù),需要再增加20條,我們就需要取出放入數(shù)組,再一次性存入100條 - Sqlite數(shù)據(jù)庫(kù)就是用來(lái)做離線緩存的
做離線緩存,目的是為了在沒(méi)有網(wǎng)絡(luò)時(shí),可以進(jìn)行加載數(shù)據(jù)
1.什么是SQLite,
SQLite是一款輕型的嵌入式數(shù)據(jù)庫(kù),占用資源非常低,在嵌入式的設(shè)備中,可能只需要幾百k的內(nèi)存就夠了.她的處理速度比Mysql postgreSQL這兩款數(shù)據(jù)庫(kù)都快.Mysql postgreSQL這兩種是大型數(shù)據(jù)庫(kù).我們手機(jī)不需要用到這種大型數(shù)據(jù)庫(kù).
2.什么數(shù)據(jù)庫(kù)
是用來(lái)存儲(chǔ)和管理數(shù)據(jù)的倉(cāng)庫(kù)
3.數(shù)據(jù)庫(kù)的兩大分類
關(guān)系型數(shù)據(jù)庫(kù)
對(duì)象性數(shù)據(jù)庫(kù)coredata
4.sqlited數(shù)據(jù)庫(kù)的優(yōu)點(diǎn)
- 操作數(shù)據(jù)比較快,讀取比較方便
- 可以局部讀取
- 比較小型,占用內(nèi)存小符合手機(jī)的特點(diǎn)≈
5.sqlited數(shù)據(jù)庫(kù)是如何存取數(shù)據(jù)的
數(shù)據(jù)庫(kù)和excel類似,是以表為單位的.表的名字
6.操作數(shù)據(jù)的步驟
- 創(chuàng)建數(shù)據(jù)庫(kù)表,表名通常以t_開(kāi)頭,比如t_student
- 設(shè)計(jì)表,根據(jù)表需要存儲(chǔ)什么數(shù)據(jù),比如,學(xué)生有學(xué)號(hào) 姓名 年齡等等
- 記錄數(shù)據(jù) 設(shè)置學(xué)生 12號(hào) 張明 15歲
- 插入其他數(shù)據(jù),13號(hào) 李敏 15歲;這種每一條信息叫一條記錄每一列叫一個(gè)字段
- 主鍵,數(shù)據(jù)庫(kù)中每一條數(shù)據(jù)的唯一標(biāo)識(shí),一般每個(gè)表只用一個(gè)主鍵
- 數(shù)據(jù)類型
Integer(整形)
real 浮點(diǎn)型 text(文本)
blob(二進(jìn)制數(shù)據(jù)比如文件挂滓,NSDate本身就是二進(jìn)制) 無(wú)類型
7.什么是SQL?
結(jié)構(gòu)化查詢語(yǔ)言是一種對(duì)關(guān)系型數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行定義和操作的語(yǔ)言
SQL語(yǔ)句是使用SQL語(yǔ)言編寫(xiě)出來(lái)的句子/代碼
在程序運(yùn)行期間,要想操作數(shù)據(jù)庫(kù)中的數(shù)據(jù)(增刪改查),必須使用SQL語(yǔ)句
8.SQL語(yǔ)句的規(guī)則
- 不區(qū)分大小寫(xiě)
- 每條語(yǔ)句都必須以;分號(hào)結(jié)尾
- 不能以關(guān)鍵字來(lái)命名表/字段
9.SQL語(yǔ)句的種類
- 數(shù)據(jù)定義語(yǔ)句DDL(data definition language)
create table drop table在數(shù)據(jù)庫(kù)中創(chuàng)建新表或刪除表
- 數(shù)據(jù)庫(kù)操作語(yǔ)句DML(data manipulation language)
insert update delete 在數(shù)據(jù)庫(kù)中對(duì)表進(jìn)行插入添加.修改.刪除表中的數(shù)據(jù) - 數(shù)據(jù)查詢語(yǔ)句DQL(data query language)
select where orderly groupie having用于查詢表中數(shù)據(jù)
10.DDL數(shù)據(jù)創(chuàng)建語(yǔ)句
創(chuàng)建表格
- create table 表名(字段1,字段類型1,字段2,字段類型2.....)
- create table if not exists 表名 (字段1 字段類型1,字段2 字段類型2)
- create table if not exists stu(s_id integer primary key autoincrement not null)順序不能調(diào)貨,主鍵是整形的只有整形才能自增
刪除表格
- drop table 表名
- drop table if exists 表名
11.DML數(shù)據(jù)庫(kù)操作語(yǔ)句----增刪改
數(shù)據(jù)庫(kù)中的字符串內(nèi)容應(yīng)該用單引號(hào)’括住
插入數(shù)據(jù)
- insert into 表名(字段名1,字段名2.奸攻。斤儿。筛圆。)values(字段1值决摧,字段2值政基。凶杖。胁艰。。)
- insert into stu (s_name,s_age) values ('aa',18)
id 是系統(tǒng)自增的
insert into mall (m_name,m_price,m_date) values ('牙膏',12.5,'20150101')
更改數(shù)據(jù)
- update 表名 set 字段名1 = 修改值1,字段名2 = 修改值2腾么, (無(wú)條件全部更新)
條件
where 字段名 條件 條件值(<,>,!=.....)
and or 關(guān)鍵詞:并且 或
- update stu set s_age = 9999 where s_age >10 and s_age<20
- update stu set s_age = 18 where s_age =1000 or s_age=9999
刪除
delete from stu 無(wú)條件全部刪除
delete from stu where s_age = 26
12.數(shù)據(jù)查詢語(yǔ)句DQL
查找 ** select * 通配符代表所有字段**
select 字段1奈梳,字段2,解虱。攘须。。from 表名 where 字段名 條件 條件值
- select * from stu where s_age = 18
- select * from stu(表名)
模糊查詢 所有字段全查
- select * from stu where s_name like ‘a(chǎn)_d’下劃線代表一個(gè)字母 所有字段全查
- select * from stu where s_name like 'a_'
- select * from stu where s_name like 'a%’不確定a后面有幾個(gè)字母用%
單查某個(gè)字段
- select s_name from stu
查找某幾個(gè)字段用逗號(hào)隔開(kāi)
- select s_name,s_age from stu
排序
- selected * from 表名 order by 字段名 升序asc/降序desc
- select * from stu order by s_age asc 按照年齡升序排列
- select * from stu where s_age > 50 order by s_age asc 年齡大于50并按照升序排列
limit 可以精確控制查詢結(jié)果的數(shù)量,比如每次只查詢10條數(shù)據(jù)
- select * from 表名 limit 數(shù)值1, 數(shù)值2; 跳過(guò)數(shù)值1條數(shù)據(jù)開(kāi)始查找數(shù)值2條
- select * from stu limit 5; 只查前5條只跟1個(gè)數(shù)值殴泰,從頭開(kāi)始查多少個(gè)
- select * from stu limit 3,7; 從第4條開(kāi)始查7條
- select * from stu where s_age > 50 order by s_age asc limit 3; 年齡大于50的3條,并按照升序排列
13.外鍵約束
利用外鍵約束創(chuàng)建表與表之間的關(guān)系
某個(gè)表的字段引用其他表的主鍵
t_students表->編輯->Foreign keys -> + -> fk_status_user
create table t_student(id integer primary key auto increment,name text,age integer,class_id integer,constraint fk_t_student_class_id_t_class_id foreign key(class_id) references t_class(id));
主鍵:唯一標(biāo)識(shí)/自增/不能為空
id integer primary key auto increment 主鍵
name text,
age integer
class_id integer
constraint約束的意思 外鍵名稱: fk_t_student_class_id_t_class_id
foreign key(class_id) references t_class(id)這個(gè)外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段
14.表連接查詢----需要聯(lián)合多張表才能查到想要的數(shù)據(jù)
表連接的類型
內(nèi)連接:inner join或者join (顯示的是左右表都有完整字段值的記錄)
外連接:left outer join (保證左表數(shù)據(jù)的連接性)
查詢0316班的所有學(xué)生(給表替換名稱,使用.語(yǔ)法)
select s.name,s.age from t_students s,t_class c where s.class_id = c.id and.name = “0316”;
以上均來(lái)自網(wǎng)上查詢于宙。