创建.xcdatamomeld文件并生成MO文件
创建数据库
[
MagicalRecord
setupCoreDataStackWithStoreNamed
:
@"MyDatabase.sqlite"
];
插入数据
Person
*person = [
Person
MR_createEntity
];
person. name = @" 马六 " ;
person. age = @22 ;
person. address = @" 上地 " ;
person. name = @" 马六 " ;
person. age = @22 ;
person. address = @" 上地 " ;
[[NSManagedObjectContext MR_defaultContext] MR_save];
查询数据
NSArray
*perarray = [
Person
MR_findAll
];
for ( Person *per in perarray) {
NSLog ( @"%@" ,per. name );
NSLog ( @"%@" ,per. age );
NSLog ( @"%@" ,per. address );
for ( Person *per in perarray) {
NSLog ( @"%@" ,per. name );
NSLog ( @"%@" ,per. age );
NSLog ( @"%@" ,per. address );
}
按条件查
NSArray
*p = [
Person
MR_findByAttribute
:
@"name"
withValue
:
@"
张三
"
];
Person *pp = [p lastObject];
修改数据
查出数据修改实体类的值载
[[NSManagedObjectContext MR_defaultContext] MR_save];保存
删除数据
查询数据调用
[pp
MR_deleteEntity
];删除数据
[[NSManagedObjectContext MR_defaultContext] MR_save];保存