iOS数据库之FMDB
来源:互联网 发布:usb3.0端口电压 编辑:程序博客网 时间:2024/06/06 01:26
FMDB是iOS平台的SQLite数据库框架,以OC的方式封装了SQLite的C语言API。
特点:
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];//判断表中是否有指定的数据, 如果没有则无删除的必要,直接returnif(![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文件代替,查询速度比较快。
0 0
- iOS数据库之FMDB
- iOS数据库操作之FMDB
- iOS数据库操作之FMDB
- ios数据库之fmdb 使用
- iOS 开发 数据库详解之FMDB
- 数据库事务 ios FMDB
- 数据库事务 ios FMDB
- ios FMDB数据库
- iOS FMDB(数据库)
- iOS FMDB 数据库~详解
- iOS开发数据库-FMDB
- iOS开发之FMDB
- iOS 存储之FMDB
- iOS之第三方FMDB框架操作数据库
- iOS之第三方FMDB框架操作数据库
- iOS数据库开发之SQLite和FMDB详解(附Demo)
- iOS数据库操作(使用FMDB)
- IOS 操作数据库(FMDB)
- STM32学习第一例
- 魔道研究
- IE8下submit表单没反应
- 算法面试题
- TOEFL(20151205)分析
- iOS数据库之FMDB
- 1. Memcached 介绍
- swift 高级用法
- PHP数据加密技术之一---CRYPT()加密
- c++动态联编与静态联编
- hdoj5167Fibonacci【map记录记忆化搜索】
- HDU 5637:Transform
- EF IN 查询等效语句
- 解析html语句的JS事件