1.NSFileHandle
<注意>只能存入和讀取字符串
2.Plist文件
<注意>只能存放7種類型的對象指針 ——開發(fā)中經(jīng)常用于存放固定不變的內(nèi)容 比如tabBar上的標題褪测、圖片等
3.NSUserDefaults
<注意>只能存放7種類型的對象指針 ——開發(fā)中經(jīng)常用于存放固定不變的內(nèi)容 比如tabBar上的標題乖酬、圖片等
NSUserDefaults支持的數(shù)據(jù)類型有:NSNumber(NSInteger、float闷旧、double)蓉冈,NSString城舞,NSDate,NSArray寞酿,NSDictionary家夺,BOOL.
NSUserDefaults適合存儲輕量級的本地數(shù)據(jù),一些簡單的數(shù)據(jù)(NSString類型的)例如密碼伐弹,網(wǎng)址等拉馋,NSUserDefaults肯定是首選,但是如果我們自定義了一個對象惨好,對象保存的是一些信息煌茴,這時候就不能直接存儲到NSUserDefaults了
NSString * name = @"lucy";
NSString * passW = @"123";
//創(chuàng)建 在沙盒路徑下 UI
NSUserDefaults * userD = [NSUserDefaults standardUserDefaults];
//第一次 在本機 登錄的時候
[userD setObject:name forKey:@"name"];
[userD setObject:passW forKey:@"passW"];
//以前的時候 把數(shù)據(jù) 同步到本地的磁盤
[userD synchronize];
//演示第二次登錄
NSUserDefaults * userS = [NSUserDefaults standardUserDefaults];
NSString * name = [userS objectForKey:@"name"];
NSString *passW = [userS objectForKey:@"passW"];
4.歸檔
<注意>能夠存放任意類型的對象指針(對象指針所屬的類必須遵守NSCoding協(xié)議 對象指針的成員變量所屬的類也要遵守NSCoding協(xié)議)
5.數(shù)據(jù)庫SQLite使用
//數(shù)據(jù)庫存放的位置在沙盒路徑
//數(shù)據(jù)庫在使用之前必須處于打開狀態(tài)
//數(shù)據(jù)庫存放在沙盒路徑下的Documents 數(shù)據(jù)庫的后綴為.db 或.sqlite
//數(shù)據(jù)庫中表格內(nèi)的數(shù)據(jù)進行增刪改查操作 有開源的第三方庫FMDataBase
//使用第三方庫對數(shù)據(jù)庫進行操作需要導入框架libsqlite3.framwork
使用sql語句只能存放系統(tǒng)類的對象指針 數(shù)據(jù)庫存放的位置在當前設(shè)備的沙盒路徑下
數(shù)據(jù)庫一般一個工程只創(chuàng)建一個
//存放本地圖片 在數(shù)據(jù)庫中的存放格式就是二進制
//存放網(wǎng)絡(luò)圖片、音頻日川、視頻 在數(shù)據(jù)庫中的存放格式就是資源的地址
blob: 二進制對象
SQL語句
create table if not exists Student (ID integer primary key autoincrement,name varchar(256),age integer,birthDay date);
//表格創(chuàng)建成功以后 里面的字段不能添加也不能刪除 否則數(shù)據(jù)修改失敗
//<1>向表格插入數(shù)據(jù)
//[注意]插入的數(shù)據(jù)除了整型蔓腐、浮點型以外的所有類型的數(shù)據(jù)全部需要用 ' '括起來
insert into Student(name,age,birthDay) values ('閩航',18,'1996-12-10');
insert into Student(name,age) values ('玲玲',18);
//查詢
//<1>查詢表格中所有數(shù)據(jù)信息
select * from Student;
insert to Student(name,age,birthDay) values ('張三',19,'1890-09-10');
//<2>查詢幾個字段的值
select name,age from Student;
//<3>按照某個條件查詢
select * from Student where name = '閩航';
//<4>按照某個條件模糊查詢
select * from Student where name like '%航%';
//<5>按照條件查詢某個字段
select birthDay from Student where name = '閩航';
//<6>查詢表格中對象的個數(shù)
select count(*) from Student;
//<7>查詢某個字段的總和
select sum(age) from Student;
//<8>查詢某個字段的平均值
select avg(age) from Student;
//修改
//<1>修改表格中所有字段的信息
update Student set name ='xuli';
select * from Student;
//<2>按照某個條件修改信息
update Student set name = '雙月' where ID=1;
update Student set name = '玲玲',age=18,birthDay='1996-10-10' where ID=2;
//<3>按照多個條件查詢
select * from Student where ID=1 and age=18;
select * from Student where ID = 1 or age = 18;
//刪除
//<1>按照條件刪除
delete from Student where ID = 1;
select * from Student;
//<2>刪除表格中的所有數(shù)據(jù)
delete from Student;
FMDB使用
//同名數(shù)據(jù)庫只能創(chuàng)建一個 如果第二次創(chuàng)建同名數(shù)據(jù)庫 會創(chuàng)建失敗 但是會將原來的數(shù)據(jù)庫打開
FMDatabase * database;
//數(shù)據(jù)庫的創(chuàng)建/表格的創(chuàng)建/增刪改 操作都用的是同一個方法executeUpdate
//只是SQL語句的內(nèi)容不同
//數(shù)據(jù)的查詢操作使用的方法是executeQuery:
//[注意]使用sql語句不能向數(shù)據(jù)庫表格中存放自定義的對象指針 只能存放系統(tǒng)提送的數(shù)據(jù)類型
使用單例方法創(chuàng)建當前類的對象 確保數(shù)據(jù)庫和表格值創(chuàng)建一次
1.單例創(chuàng)建數(shù)據(jù)庫
//創(chuàng)建數(shù)據(jù)庫
NSString * path = [NSHomeDirectory() stringByAppendingString:@"/Documents/PersonDB.db"];
//初始化對象
FMDatabase * fmdb = [[FMDatabase alloc]initWithPath:path];
//打開數(shù)據(jù)庫
BOOL isSuccess = [fmdb open];
if (isSuccess) {
NSString * sql = @"create table if not exists UserInfo(ID integer primary key,name varchar(256),age integer,image blob)";
BOOL isTable = [fmdb executeUpdate:sql];
if (isTable) {
NSLog(@"success");
}else
{
NSLog(@"%@",fmdb.lastErrorMessage);
}
}else{
NSLog(@"數(shù)據(jù)庫創(chuàng)建/打開失敗%@",fmdb.lastErrorMessage);
}
2.增
int personID = person.personID;
NSString * personName = person.personName;
int age = person.personAge;
UIImage * personImage = person.personHeadImage;
NSString * sql = @"insert into UserInfo(ID,name,age,image) values(?,?,?,?)";
BOOL isSucess = [fmdb executeUpdate:sql,@(personID),personName,@(age),UIImagePNGRepresentation(personImage)];
if (isSucess) {
NSLog(@"insert success");
}
3.刪
NSString * sql = @"delete from Person where name = ?";
BOOL isSuccess = [database executeUpdate:sql,@"wang"];
if (isSuccess) {
NSLog(@"delete success");
}else{
NSLog(@"delete false");
}
4.改
NSString * sql = @"update UserInfo set name = ?,age = ?,image = ? where ID = ?";
BOOL isSuccess = [fmdb executeUpdate:sql,person.personName,@(person.personAge),UIImagePNGRepresentation(person.personHeadImage),@(person.personID)];
if (isSuccess) {
NSLog(@"update success");
}
5.查
NSString * sql = @"select * from UserInfo";
FMResultSet * result = [fmdb executeQuery:sql];
//定義一個數(shù)組存放表格中的所有對象
NSMutableArray * allUserInfo = [[NSMutableArray alloc]init];
while ([result next]) {
//遍歷表格的一行就是一個對象
PersonInfo * person = [[PersonInfo alloc]init];
person.personID = [result intForColumn:@"ID"];
person.personName = [result stringForColumn:@"name"];
person.personAge = [result intForColumn:@"age"];
person.personHeadImage = [UIImage imageWithData:[result dataForColumn:@"image"]];
[allUserInfo addObject:person];
}
return allUserInfo;
6.CoreData使用
coreData能夠存放任意類型的對象指針包括自定義的對象指針