iOS数据库之FMDB
2016-03-07 21:47
337 查看
FMDB是iOS平台的SQLite数据库框架,以OC的方式封装了SQLite的C语言API。
特点:
1.使用起来更加面向对象,省去了很多麻烦
2.冗余的C语言代码,对比苹果自带的Core Data框架,更加轻量级和灵活
3.提供了多线程安全的数据库操作方法,有效地防止数据混乱
使用步骤
1.首先要下载一个FMDB库,导入第三方类库FMdatabase
2.获得存放数据库文件的沙盒地址
3.创建一个数据库
4.创建表
5.添加表数据
6.删除数据
8.查询数据
注意:如果储存数量比较少的情况下,可以用plist文件代替,查询速度比较快。
特点:
1.使用起来更加面向对象,省去了很多麻烦
2.冗余的C语言代码,对比苹果自带的Core Data框架,更加轻量级和灵活
3.提供了多线程安全的数据库操作方法,有效地防止数据混乱
使用步骤
1.首先要下载一个FMDB库,导入第三方类库FMdatabase
2.获得存放数据库文件的沙盒地址
+(NSString *)databaseFilePath { //获取沙盒路径 NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = [filePath objectAtIndex:0]; NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"db.sqlite"]; return dbFilePath; }
3.创建一个数据库
+(void)creatDatabase { db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain]; }
4.创建表
+(void)creatTable { //先判断数据库是否存在,如果不存在,创建数据库 if (!db) { [self creatDatabase]; } //判断数据库是否已经打开,如果没有打开,提示失败 if (![db open]) { NSLog(@"数据库打开失败"); return; } //为数据库设置缓存,提高查询效率 [dbsetShouldCacheStatements:YES]; //判断数据库中是否已经存在这个表,如果不存在则创建该表 if(![dbtableExists:@"people"]) { [db executeUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "]; NSLog(@"创建完成"); } }
5.添加表数据
+(void)insert:(People *)aPeople { if (!db) { [self creatDatabase]; } if (![db open]) { NSLog(@"数据库打开失败"); return; } [dbsetShouldCacheStatements:YES]; if(![db tableExists:@"people"]) { [self creatTable]; } //以上操作与创建表是做的判断逻辑相同 //现在表中查询有没有相同的元素,如果有,做修改操作 FMResultSet *rs = [dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; if([rs next]) { [db executeUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; } //向数据库中插入一条数据 else{ [dbexecuteUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; } }
6.删除数据
+(void)deletePeopleByID:(int)ID { if (!db) { [selfcreatDatabase]; } if (![db open]) { NSLog(@"数据库打开失败"); return; } [dbsetShouldCacheStatements:YES]; //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return if(![dbtableExists:@"people"]) { return; } //删除操作 [db executeUpdate:@"delete from people where people_id = ?", [NSStringstringWithFormat:@"%d",ID]]; [db close]; }7.更新数据
+(void)update:(People *)aPeople { if (!db) { [self creatDatabase]; } if (![db open]) { NSLog(@"数据库打开失败"); return; } [db setShouldCacheStatements:YES]; if(![db tableExists:@"people"]) { [self creatTable]; } //现在表中查询有没有相同的元素,如果有,做修改操作 FMResultSet *rs = [dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; if([rs next]) { [db executeUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; }else{<pre name="code" class="objc" style="color: rgb(70, 70, 70); font-size: 14px;">[dbexecuteUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]];}
8.查询数据
+(NSArray *)getAllPeople { if (!db) { [self creatDatabase]; } if (![db open]) { NSLog(@"数据库打开失败"); return nil; } [dbsetShouldCacheStatements:YES]; if(![dbtableExists:@"people"]) { return nil; } //定义一个可变数组,用来存放查询的结果,返回给调用者 NSMutableArray *peopleArray = [[NSMutableArrayalloc] initWithArray:0]; //定义一个结果集,存放查询的数据 FMResultSet *rs = [dbexecuteQuery:@"select * from people"]; //判断结果集中是否有数据,如果有则取出数据 while ([rs next]) { People *aPeople = [[People alloc] init]; aPeople.peopleID = [rs intForColumn:@"people_id"]; aPeople.name = [rs stringForColumn:@"name"]; aPeople.age = [rs intForColumn:@"age"]; //将查询到的数据放入数组中。 [peopleArray addObject:aPeople]; } return [peopleArray autorelease]; }
注意:如果储存数量比较少的情况下,可以用plist文件代替,查询速度比较快。
相关文章推荐
- 1. Memcached 介绍
- 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed
- 突然想起MySql乱码问题
- SQL语句增删改查
- PHP笔记(一)安装配置过程:memcache
- MySQL学习笔记3(创建、修改、删除表)
- MySQL引擎
- jdbc 连接 mysql
- Xcode 7 使用CoreData 关于数据库升级问题
- ORACLE基础知识
- MSSQL 构建临时表SQL
- MSSQL DBOtherSQL
- MongoDB利用正则表达式查询(pymongo实现)
- ElasticSearch学习_陶文3_时间序列数据库的秘密(3)——加载和分布式计算
- 三.通过Model建立数据库信息
- java操作mongodb数据库
- 图数据库neo4j初探
- sql中的group by 和 having 用法解析
- sql查询相关技术-内连接查询
- MySQL批量SQL插入性能优化