详解iOS应用开发中Core Data数据存储的使用


当前第2页 返回上一页

7.删除数据

代码如下:

/* Create the fetch request first */ 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    /* Here is the entity whose contents we want to read */ 
    NSEntityDescription *entity = 
    [NSEntityDescription 
     entityForName:@"Person" 
     inManagedObjectContext:self.managedObjectContext]; 
    /* Tell the request that we want to read the
     contents of the Person entity */ 
    [fetchRequest setEntity:entity]; 
    NSError *requestError = nil; 
    /* And execute the fetch request on the context */ 
    NSArray *persons = 
    [self.managedObjectContext executeFetchRequest:fetchRequest 
                                             error:&requestError]; 
    if ([persons count] > 0){ 
        /* Delete the last person in the array */ 
        Person *lastPerson = [persons lastObject]; 
        [self.managedObjectContext deleteObject:lastPerson]; 
        NSError *savingError = nil; 
        if ([self.managedObjectContext save:&savingError]){ 
            NSLog(@"Successfully deleted the last person in the array."); 
        } else { 
            NSLog(@"Failed to delete the last person in the array."); 
        } 
    } else {  
        NSLog(@"Could not find any Person entities in the context.");  
    }  

8.排序

代码如下:

<pre code_snippet_id="243955" snippet_file_name="blog_20140319_5_4289257" name="code" class="objc">NSSortDescriptor *ageSort =  
[[NSSortDescriptor alloc] initWithKey:@"age"  
ascending:YES];  
NSSortDescriptor *firstNameSort =  
[[NSSortDescriptor alloc] initWithKey:@"firstName"  
ascending:YES];  
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:  
ageSort,  
firstNameSort, nil nil];  
fetchRequest.sortDescriptors = sortDescriptors; </pre><p></p> 
<pre></pre> 
<p></p> 
<p style="background-color:rgb(255,255,255); margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,'ms song',Arial,Helvetica,sans-serif; line-height:19.09090805053711px"> 
<span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br> 
</span></span></p> 
<span style="background-color:rgb(255,255,255)">注意</span><span style="font-size:18px; background-color:rgb(255,255,255)">ascending:YES 属性决定排序顺序</span><span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br> 
<br> 
<br> 
</span></span><br> 


标签:SQLite

返回前面的内容

相关阅读 >>

django 连接数据库出现1045错误的解决方式

简单分析Sqlite4的一些设计改变

python使用Sqlite3第三方库读写Sqlite数据库的方法步骤

sqlserver、mysql、oracle三种数据库的优缺点总结

python执行数据库的查询操作实例讲解

Sqlite3 命令行操作指南

python web框架之django框架model基础详解

android Sqlite命令详解(基本命令)

Sqlite developer怎么安装激活 Sqlite developer激活安装图文教程

android Sqlite设置主键自增长的方法教程

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...