iOS关于数据库的操作(使用第三方类库FMDB)
2013-09-18 17:09
495 查看
首先要先导入第三方类库FMdatabase
获得存放数据库文件的沙盒地址
+(NSString *)databaseFilePath
{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath
= [filePath objectAtIndex:0];
NSLog(@"%@",filePath);
NSString *dbFilePath
= [documentPath stringByAppendingPathComponent:@"db.sqlite"];
return dbFilePath;
}
创建数据库的操作
+(void)creatDatabase
{
db= [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain];
}
创建表
+(void)creatTable
{
//先判断数据库是否存在,如果不存在,创建数据库if(!db)
{
[selfcreatDatabase];
}
//判断数据库是否已经打开,如果没有打开,提示失败
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(@"创建完成");
}
}
增加表数据
+(void)insertPeople:(People*)aPeople
{
if(!db)
{
[selfcreatDatabase];
}
if(![db open])
{
NSLog(@"数据库打开失败");
return;
}
[dbsetShouldCacheStatements:YES];
if(![dbtableExists:@"people"])
{
[selfcreatTable];
}
//以上操作与创建表是做的判断逻辑相同//现在表中查询有没有相同的元素,如果有,做修改操作
FMResultSet*rs = [dbexecuteQuery:@"select
* from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]];
if([rs next]) {
NSLog(@"dddddslsdkien");
[dbexecuteUpdate:@"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]];
}
}
删除数据
+(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];
}
修改操作与增加操作的步骤一致
查询
+(NSArray*)getAllPeople
{
if(!db)
{
[selfcreatDatabase];
}
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];
}
原文地址:http://www.cnblogs.com/lzz900201/archive/2012/11/06/2756284.html
获得存放数据库文件的沙盒地址
+(NSString *)databaseFilePath
{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath
= [filePath objectAtIndex:0];
NSLog(@"%@",filePath);
NSString *dbFilePath
= [documentPath stringByAppendingPathComponent:@"db.sqlite"];
return dbFilePath;
}
创建数据库的操作
+(void)creatDatabase
{
db= [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain];
}
创建表
+(void)creatTable
{
//先判断数据库是否存在,如果不存在,创建数据库if(!db)
{
[selfcreatDatabase];
}
//判断数据库是否已经打开,如果没有打开,提示失败
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(@"创建完成");
}
}
增加表数据
+(void)insertPeople:(People*)aPeople
{
if(!db)
{
[selfcreatDatabase];
}
if(![db open])
{
NSLog(@"数据库打开失败");
return;
}
[dbsetShouldCacheStatements:YES];
if(![dbtableExists:@"people"])
{
[selfcreatTable];
}
//以上操作与创建表是做的判断逻辑相同//现在表中查询有没有相同的元素,如果有,做修改操作
FMResultSet*rs = [dbexecuteQuery:@"select
* from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]];
if([rs next]) {
NSLog(@"dddddslsdkien");
[dbexecuteUpdate:@"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]];
}
}
删除数据
+(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];
}
修改操作与增加操作的步骤一致
查询
+(NSArray*)getAllPeople
{
if(!db)
{
[selfcreatDatabase];
}
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];
}
原文地址:http://www.cnblogs.com/lzz900201/archive/2012/11/06/2756284.html
相关文章推荐
- iOS关于数据库的操作(使用第三方类库FMDB)
- iOS关于数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- iOS关于SQLite数据库的操作(使用第三方类库FMDB)
- IOS-20-数据库操作之(SQLite第三方封装库FMDB)的基础入门使用及demo源码
- iOS学习笔记——数据库操作(使用FMDB)
- iOS之第三方FMDB框架操作数据库
- ios数据存储中关于Sqlite3使用第三方框架FMDB
- iOS学习笔记(十六)——数据库操作(使用FMDB)
- iOS 使用FMDB进行数据库操作
- iOS学习笔记 数据库操作(使用FMDB)
- iOS学习笔记(十六)——数据库操作(使用FMDB)
- 关于cocoapods的使用(管理ios开发第三方类库)
- iOS中使用FMDB对数据库的操作(二)
- iOS学习笔记(十六)——数据库操作(使用FMDB)
- iOS第三方数据库FMDB的使用介绍
- iOS学习笔记(十六)——数据库操作(使用FMDB)
- sqlite语句,关于使用FMDB操作带有外键的数据库