1.导入CoreData.framework创建Data Model
2.添加Entities和Attributes
3.创建对应的类
4.创建管理类
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface SqliteViewController : UIViewController
//数据模型对象
@property(strong,nonatomic) NSManagedObjectModel *managedObjectModel;
//上下文对象
@property(strong,nonatomic) NSManagedObjectContext *managedObjectContext;
//持久性存储区
@property(strong,nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//初始化Core Data使用的数据库
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator;
//managedObjectModel的初始化赋值函数
-(NSManagedObjectModel *)managedObjectModel;
//managedObjectContext的初始化赋值函数
-(NSManagedObjectContext *)managedObjectContext;
//具体操作
//添加数据
-(void)addCoreData;
//查询
-(NSArray*)searchCoreData;
//删除
-(void)delegateCoreData;
--------------------------------------------------------------------------------------------------------------------------------------#import "SqliteViewController.h"
#import "Message.h"
@interface SqliteViewController ()
@end
@implementation SqliteViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self managedObjectContext];
}
-(NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil] ;
return _managedObjectModel;
}
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
//得到数据库的路径
NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//CoreData是建立在SQLite之上的,数据库名称需与Xcdatamodel文件同名
NSURL *storeUrl = [NSURL fileURLWithPath:[docs stringByAppendingPathComponent:@"ZhiHui.sqlite"]];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Error: %@,%@",error,[error userInfo]);
}
return _persistentStoreCoordinator;
}
-(NSManagedObjectContext * )managedObjectContext
{ NSLog(@"-------");
NSLog(@"self.context = %@",_managedObjectContext);
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator =[self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc]init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
#pragma mark---------------数据库具体操作
//添加数据
-(void)addCoreData{
Message * mess = [NSEntityDescription insertNewObjectForEntityForName:@"Message" inManagedObjectContext:self.managedObjectContext];
mess.idn = @10;
mess.name = @"name1";
mess.address = @"address1";
NSLog(@"存储数据:%d",[self.managedObjectContext save:nil]);
}
//查询
-(NSArray*)searchCoreData{
NSFetchRequest * request = [[NSFetchRequest alloc]init];
request.entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:self.managedObjectContext];
return [self.managedObjectContext executeFetchRequest:request error:nil];
}
//删除
-(void)delegateCoreData{
Message * mess = [[self searchCoreData]lastObject];
[self.managedObjectContext deleteObject:mess];
[self.managedObjectContext save:nil];
}