1,引入数据库
这里是使用cocoapods导入,手动导入以后有时间会去做
2,查看数据库路径,以及查看数据库的文件
路径一般都是在Documents里面,打印方法:
NSString *path = NSHomeDirectory();//主目录
NSLog(@"NSHomeDirectory:%@",path);
NSString *userName = NSUserName();//与上面相同
NSString *rootPath = NSHomeDirectoryForUser(userName);
NSLog(@"NSHomeDirectoryForUser:%@",rootPath);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];//Documents目录
NSLog(@"NSDocumentDirectory:%@",documentsDirectory);
找到数据库的路径,创建成功的话,能看到以.realm 结尾的文件,就是数据库文件,打开该文件的软件我用的是:Realm Browser。 在app store里面可以搜索到的,是免费软件
3,创建数据库
如果是第一次创建数据库,就不用判断版本。但是如果不是第一次建立数据库,比如升级app的时候,在新版本的app数据库中添加了几个属性,这时候就要判断app内数据库的版本,因为如果不判断版本,就会出现崩溃的现象,代码:《一般会在判断版本数据库路径最后,创建数据库这里只是把创建的方法单独提出来了》
//获取数据库路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealmConfiguration * config = [RLMRealmConfiguration defaultConfiguration];
//最新版本,每次更新,都要在上次的基础上加一,不能低于之前的版本
config.schemaVersion = 3;
//配置新路径
config.fileURL = [NSURL URLWithString:pathName];
config.migrationBlock = ^(RLMMigration * _Nonnull migration, uint64_t oldSchemaVersion) {
if (oldSchemaVersion < 3) {
NSLog(@"进行数据迁移");
}else{
NSLog(@"不进行数据迁移");
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
创建数据库:
默认版本创建:
RLMRealm * realm = [RLMRealm defaultRealm];
自定义创建:
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
4,添加数据
首先创建模型
m文件的内容,必不可少
h文件的内容
首先是添加一种数据到数据库,没有包含关系的:
SYDog * dog = [[SYDog alloc]init];
dog.name = [NSString stringWithFormat:@"dog_%u",arc4random()%10];
dog.owner = [NSString stringWithFormat:@"%d",i];
dog.sex = arc4random()%2;
NSInteger cun = self.colorArray.count;
dog.colorStr = self.colorArray[arc4random()%cun];
//默认数据库路径以及名字
/*
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:dog];
}];
*/
//自定义数据库名字和路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
[realm transactionWithBlock:^{
[realm addObject:dog];
}];
添加包含数组的数据到数据库《这里的思路跟添加单个差不多,就相当于添加完单个的,再把单个模型作为一个元素添加到数组中》:
SYPreson * presen = [[SYPreson alloc]init];
presen.name = [NSString stringWithFormat:@"地主——%d",i];
presen.age = arc4random()%20;
if (arc4random()%2 == 1) {
presen.isMan = YES;
}else{
presen.isMan = NO;
}
NSInteger a = arc4random()%self.palceArray.count;
presen.adress = [NSString stringWithFormat:@"%@",self.palceArray[a]];
for (int i = 0; i < 10; i++) {
SYDog * dog = [[SYDog alloc]init];
dog.name = [NSString stringWithFormat:@"dog_%u",arc4random()%10];
dog.owner = [NSString stringWithFormat:@"%d",i];
dog.sex = arc4random()%2;
NSInteger cun = self.colorArray.count;
dog.colorStr = self.colorArray[arc4random()%cun];
[presen.dogs addObject:dog];
}
SYClass * clas = [[SYClass alloc]init];
NSInteger year = arc4random()%8;
NSInteger ca = arc4random()%50;
clas.className = [NSString stringWithFormat:@"%ld年级 %ld班",year,ca];
clas.classNumber = [NSString stringWithFormat:@"%u",arc4random()%120];
[presen.classs addObject:clas];
//默认数据库路径
/*
RLMRealm * realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:presen];
}];
*/
//自定义数据库名字和路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
[realm transactionWithBlock:^{
[realm addObject:presen];
}];
查询数据库:
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
//查询所有
RLMResults * presons = [SYPreson allObjectsInRealm:realm];
// NSLog(@" === %@",presons);
for (SYPreson * pre in presons) {
// NSLog(@"pwer == %@",pre.name);
}
//按照年龄查询,年龄为int类型
RLMResults * ages = [SYPreson objectsInRealm:realm where:@"age = 12"];
for (SYPreson * age in ages) {
// NSLog(@"age === %@ %ld",age.name,age.age);
}
//按照名字查询,名字是string类型
RLMResults * dogs = [SYDog objectsInRealm:realm where:@"name = 'dog_2'"];
for (SYDog * dog in dogs) {
// NSLog(@"dog === %@ %@",dog.name,dog.owner);
}
//多种条件查询
RLMResults * res = [SYPreson objectsInRealm:realm where:@"age < 12 AND adress = '秦朝'"];
for (SYPreson * p in res) {
// NSLog(@"------ %@ %@ %ld",p.name,p.adress,p.age);
}
//断言查询,查询速度比较快,推荐使用
//断言查询单个数据
NSPredicate * pre = [NSPredicate predicateWithFormat:@"ANY dogs.sex = %ld",1];
RLMResults * resPre = [SYPreson objectsInRealm:realm withPredicate:pre];
for (SYPreson * pres in resPre) {
// NSLog(@"------ %@ %@ %ld",pres.name,pres.adress,pres.age);
}
//断言查询多个数据条件
NSPredicate * pres = [NSPredicate predicateWithFormat:@"adress = %@",@"秦朝"];
NSPredicate * presN = [NSPredicate predicateWithFormat:@"ANY dogs.name = %@",@"dog_3"];
//链式查询,
RLMResults * resPres = [[SYPreson objectsInRealm:realm withPredicate:pres] objectsWithPredicate:presN];
for (SYPreson * pr in resPres) {
NSLog(@"------ %@ %@ %ld",pr.name,pr.adress,pr.age);
}
修改和删除都是在查询的基础上进行的操作,这里只写出了简单的修改和删除操作
修改数据库
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
//查询出年龄为1的person
RLMResults * per = [SYPreson objectsInRealm:realm where:@"age = 1"];
for (SYPreson * p in per) {
[realm transactionWithBlock:^{
p.name = @"年龄为1,修改名字";
}];
}
删除数据库
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];
RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];
//删除某一类元素,这里的查询操作跟上面查询操作一样,查询到单个或者多个数据,使用delete方法直接删除
RLMResults * ages = [SYPreson objectsInRealm:realm where:@"age > 14"];
[realm transactionWithBlock:^{
[realm deleteObjects:ages];
}];
//直接删除所有数据
RLMResults * per = [SYPreson allObjectsInRealm:realm];
[realm transactionWithBlock:^{
//单独删除每个数据,如果要删除所有的,要使用循环删除
// [realm deleteObject:per.lastObject];
//一次性删除一个数组的数据
[realm deleteObjects:per];
}];
RLMResults * dogs = [SYDog allObjectsInRealm:realm];
[realm transactionWithBlock:^{
[realm deleteObjects:dogs];
}];
简单的增删改查,关于realm更深层次的用法会在以后慢慢更新
realm 官网地址:realm