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
原创粉丝点击