iOS之数据库-- coredata数据库操作

//

//  ViewController.m

//  schcoredatasqlite

//

//  Created by tianshangrenjian on 15/6/25.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import "Student+CoreDataProperties.h"



@interface ViewController ()

@property (strong,nonatomic) NSManagedObjectContext *ctx;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //

    

    //创建模型

    NSManagedObjectModel *model=[NSManagedObjectModel mergedModelFromBundles:nil];

    //创建持久化器

    NSPersistentStoreCoordinator *persist=[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

    

    NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSURL *url1=[NSURL fileURLWithPath:[path stringByAppendingPathComponent:@"stu.sqlite"]];

   

    NSLog(@"%@",[path stringByAppendingPathComponent:@"stu.sqlite"]);

 //   [persist persistentStoreForURL:url1];

    //告诉持久化器,当前保存的数据类型

    [persist addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url1 options:nil error:nil];

    

    //创建对象上下文,模型数据和数据库之间的桥梁

    self.ctx=[[NSManagedObjectContext alloc] initWithConcurrencyType:NSManagedObjectExternalRelationshipError];

    

    //设置上下文存储数据时所使用的持久化器

    self.ctx.persistentStoreCoordinator=persist;

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)btnadd:(id)sender {

    //根据模型说明在上下文中,创建一个该模型的对象

    Student *stu=[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.ctx];

    stu.name=@"bbc";

    stu.gender=@"man";

    stu.age=@"20";

    [self.ctx save:nil];

    

    [self selectStudent];

}

- (void)selectStudent

{

    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Student"];

    NSArray *array=[self.ctx executeFetchRequest:request error:nil];

    for (Student *stu in array) {

        NSLog(@"%@   %@   %@",stu.name,stu.gender,stu.age);

    }

}

- (IBAction)btnchange:(id)sender {

    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Student"];

    

    NSPredicate *p=[NSPredicate predicateWithFormat:@"gender=%@",@"man"];

    request.predicate=p;

    

    NSArray *array=[self.ctx executeFetchRequest:request error:nil];

    

    Student *stu=[array firstObject];

    

    stu.name=@"fuckyou";

    [self.ctx save:nil];

    

    [self selectStudent];

}

- (IBAction)btnDel:(id)sender {

    [self selectStudent];

    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Student"];//select * from student

    //查询条件过滤

    NSPredicate *p=[NSPredicate predicateWithFormat:@"gender=%@",@"man"];//where gender='feman'

    

    request.predicate=p;//select * from student where gender='feman'

    NSArray *array=[self.ctx executeFetchRequest:request error:nil];

 

    if (array==nil) {

        return;

    }

    //删除查询结果中的第一个行

    [self.ctx deleteObject:[array firstObject]];

  

    

    //删除完毕之后必须保存,否则结果不不放回数据库

    [self.ctx save:nil];

    NSLog(@"begin delete:");

    [self selectStudent];

    

    

    

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值