您的位置:首页 > 数据库

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.获得存放数据库文件的沙盒地址

+(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文件代替,查询速度比较快。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: