- 創(chuàng)建項(xiàng)目的時(shí)候勾選上
Paste_Image.png
- 勾選后會(huì)自動(dòng)生成數(shù)據(jù)庫文件
Paste_Image.png
- 點(diǎn)擊后再點(diǎn)擊"加號(hào)"添加實(shí)體(entity)
- 實(shí)體(entity)的屬性與要?jiǎng)?chuàng)建的類的屬性要一致
Paste_Image.png
- 左側(cè)Person首字母要大寫,否則報(bào)錯(cuò).
- 右側(cè)屬性名要小寫否則報(bào)錯(cuò),這可能是xcode區(qū)分 "類" 與 "屬性" 的機(jī)制
- Attributes屬性對(duì)應(yīng)的類型勾選上
- 下面Relationship關(guān)系中,第一個(gè) sex 代表Person中有sex屬性,Relationship第二個(gè)參數(shù)Sex (注意大寫的),代表sex屬性所屬與Sex類,第三個(gè)"inverse"代表的是Sex類中含有Person類型的屬性
- Sex的entity中也一樣
Paste_Image.png
- 第三步創(chuàng)建與數(shù)據(jù)庫相關(guān)聯(lián)的類文件
Paste_Image.png
- 點(diǎn)擊后
Paste_Image.png
- 勾選,next
Paste_Image.png
- 這一步一定要全選,點(diǎn)擊next后生成八個(gè)文件
Paste_Image.png
- 暫時(shí)不要?jiǎng)舆@幾個(gè)文件
- 下面寫代碼:
- 在Appdelegate中可以發(fā)現(xiàn)生成了好多方法,這里是固定寫法不用太在意,在這里解釋一下主要的幾個(gè)對(duì)象的作用
//_managedObjectContext這是管理數(shù)據(jù)庫中所有實(shí)體的上下文,也就是通過他可以查找數(shù)據(jù)庫中的數(shù)據(jù)
@synthesize managedObjectContext = _managedObjectContext;
//對(duì)象模型,根據(jù)對(duì)象模型來存儲(chǔ)數(shù)據(jù)
@synthesize managedObjectModel = _managedObjectModel;
//持久化數(shù)據(jù)存儲(chǔ)調(diào)度器,也就是通過它可以把數(shù)據(jù)存到數(shù)據(jù)庫中
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- 就做了一個(gè)簡(jiǎn)單的界面
Paste_Image.png
輸入所有的數(shù)據(jù)后點(diǎn)擊保存可以把數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫
當(dāng)輸入查詢的內(nèi)容為空的時(shí)候,點(diǎn)擊查詢會(huì)列出所有數(shù)據(jù)庫的內(nèi)容
當(dāng)輸入查詢內(nèi)容后,點(diǎn)擊刪除,會(huì)刪掉所有,包含輸入的內(nèi)容的字母的對(duì)象
當(dāng)輸入查詢內(nèi)容后,點(diǎn)擊修改,會(huì)修改掉所有,包含輸入的內(nèi)容的字母的對(duì)象,修改的內(nèi)容統(tǒng)一設(shè)置了一下.可以根據(jù)自己需要做出修改.
看控制器代碼:可以做一些略微的封裝.大概看一下
第一個(gè)功能增加數(shù)據(jù): 根據(jù)輸入的內(nèi)容保存到數(shù)據(jù)庫
/** 保存 增加數(shù)據(jù) */
- (IBAction)saveClick:(id)sender {
//獲取AppDelegate中的被管理所有對(duì)象的上下文managedObjectContext
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = delegate.managedObjectContext;
//創(chuàng)建對(duì)象,來分別保存輸入的內(nèi)容
Person * per = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
//獲取輸入的各項(xiàng)內(nèi)容
per.name = self.name.text;
per.age = self.age.text;
per.phone = self.phone.text;
//創(chuàng)建sex對(duì)象,把sex對(duì)象賦值給Perosn中的sex屬性,同時(shí)保存sex
Sex * sex = [NSEntityDescription insertNewObjectForEntityForName:@"Sex" inManagedObjectContext:context];
sex.sex = self.sex.text;
per.sex = sex;
//保存數(shù)據(jù)
NSLog(@"保存");
[delegate saveContext];
}
- 第二個(gè)功能:查詢數(shù)據(jù)
/** 查詢 */
- (IBAction)search:(id)sender {
NSString * searchContent = self.searchTxt.text;
NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",searchContent];
/**
1.謂詞查詢內(nèi)容
2.如果輸入要查詢的內(nèi)容長(zhǎng)度為0,則顯示保存的所有的數(shù)據(jù)
*/
if (searchContent.length == 0) {
pre = nil;
}
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = delegate.managedObjectContext;
NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest * request = [NSFetchRequest new];
request.entity = des;
request.predicate = pre;
/** 獲取查詢到的內(nèi)容---是數(shù)組 */
NSArray * array = [context executeFetchRequest:request error:NULL];
//遍歷查詢到的數(shù)據(jù),并打印
for (Person * p in array) {
NSLog(@" %@ : %@ : %@ : %@",p.name,p.age,p.sex.sex,p.phone);
}
}
- 第三個(gè)功能: 刪除
/** 刪除 */
- (IBAction)delete:(id)sender {
/**
1.查詢到要?jiǎng)h除的內(nèi)容
2.獲取輸入的內(nèi)容
*/
NSString * deleteContent = self.searchTxt.text;
/** 查詢要?jiǎng)h除帶有輸入的關(guān)鍵字的對(duì)象 */
NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",deleteContent];
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
/** 被管理對(duì)象上下文--(獲取所有被管理對(duì)象的實(shí)體) */
NSManagedObjectContext * context = delegate.managedObjectContext;
/** 根據(jù)上下文獲取查詢數(shù)據(jù)庫實(shí)體的請(qǐng)求參數(shù)---要查詢的entity(實(shí)體) */
NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
/** 查詢請(qǐng)求 */
NSFetchRequest * request = [NSFetchRequest new];
/** 根據(jù)參數(shù)獲取查詢內(nèi)容 */
request.entity = des;
request.predicate = pre;
/**
1.獲取所有被管理對(duì)象的實(shí)體---根據(jù)查詢請(qǐng)求取出實(shí)體內(nèi)容
2.獲取的查詢內(nèi)容是數(shù)組
3.刪掉所有查詢到的內(nèi)容
3.1.這里是模糊查詢 即 刪除包含要查詢內(nèi)容的字母的內(nèi)容
*/
NSArray * array = [context executeFetchRequest:request error:NULL];
/** 對(duì)查詢的內(nèi)容進(jìn)行操作 */
for (Person * p in array) {
[context deleteObject:p];
}
NSLog(@"刪除完成");
[delegate saveContext];
}
- 第四個(gè)功能:修改
/** 修改 */
- (IBAction)update:(id)sender {
/** 獲取輸入內(nèi)容 */
NSString * updateContent = self.searchTxt.text;
NSPredicate * pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",updateContent];
AppDelegate * delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext * context = delegate.managedObjectContext;
NSEntityDescription * des = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSFetchRequest * request = [NSFetchRequest new];
request.entity = des;
request.predicate = pre;
NSArray * array = [context executeFetchRequest:request error:NULL];
//這里修改的話把全部查詢到的內(nèi)容修改成了 "張三",可以根據(jù)自己的需要進(jìn)行設(shè)置
for (Person *p in array) {
p.name = @"張三";
[context updatedObjects];
}
NSLog(@"修改完成");
[delegate saveContext];
}
謂詞使用小結(jié):
Paste_Image.png
Paste_Image.png
Paste_Image.png
簡(jiǎn)單的增刪改查完成,感興趣的可以做一下tableView的cell,對(duì)cell的數(shù)據(jù)進(jìn)行增刪改查,設(shè)置代理,實(shí)時(shí)監(jiān)聽數(shù)據(jù)庫數(shù)據(jù)的變化,來實(shí)時(shí)刷新tableView即可