1、首先要先导入第三方类库FMdatabase。
2、获得存放数据库文件的沙盒地址。
| +(NSString*)databaseFilePath | |
| 2 | { |
| 3 | |
| 4 |
NSArray*filePath=NSSearchPathForDirectori |
| 5 | NSString*documentPath=[filePathobjectAtIndex:0]; |
| 6 | NSLog(@"%@",filePath); |
| 7 |
NSString*dbFilePath=[documentPathstringByAppendingPathCom |
| 8 | returndbFilePath; |
| 9 | |
| 10 | } |
3、创建数据库的操作
| +(void)creatDatabase | |
| 2 | { |
| 3 |
db
=[[FMDatabasedatabaseWithPa |
| 4 | } |
4、创建表
| +(void)creatTable | |
| 2 | { |
| 3 | //先判断数据库是否存在,如果不存在,创建数据库 |
| 4 | if(!db){ |
| 5 | [selfcreatDatabase]; |
| 6 | } |
| 7 | //判断数据库是否已经打开,如果没有打开,提示失败 |
| 8 | if(![dbopen]){ |
| 9 | NSLog(@"数据库打开失败"); |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | //为数据库设置缓存,提高查询效率 |
| 14 |
[dbsetShouldCacheStatemen |
| 15 | |
| 16 | //判断数据库中是否已经存在这个表,如果不存在则创建该表 |
| 17 | if(![dbtableExists:@"people"]) |
| 18 | { |
| 19 | [dbexecuteUpdate:@"CREATETABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, nameTEXT, age INTEGER) "]; |
| 20 | |
| 21 | |
| 22 | NSLog(@"创建完成"); |
| 23 | } |
| 24 | |
| 25 | } |
5、增加表数据
| +(void)insertPeople:(People*)aPeople | |
| 2 | { |
| 3 | if(!db){ |
| 4 | [selfcreatDatabase]; |
| 5 | } |
| 6 | |
| 7 | if(![dbopen]){ |
| 8 | NSLog(@"数据库打开失败"); |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 |
[dbsetShouldCacheStatemen |
| 13 | |
| 14 | if(![dbtableExists:@"people"]) |
| 15 | { |
| 16 | [selfcreatTable]; |
| 17 | } |
| 18 | //以上操作与创建表是做的判断逻辑相同 |
| 19 | //现在表中查询有没有相同的元素,如果有,做修改操作 |
| 20 |
FMResultSet*rs=[dbexecuteQuery:@"select*
from people where people_id = ?",[NSStringstringWithFormat |
| 21 | if([rsnext]) |
| 22 | { |
| 23 | NSLog(@"dddddslsdkien"); |
| 24 |
[dbexecuteUpdate:@"updatepeople
set name = ?, age = ? where people_id =1",aPeople.name,[NSStringstringWithFormat |
| 25 | } |
| 26 | //向数据库中插入一条数据 |
| 27 | else{ |
| 28 |
[dbexecuteUpdate:@"INSERTINTO
people (name, age) VALUES(?,?)",aPeople.name,[NSStringstringWithFormat |
| 29 | } |
| 30 | |
| 31 | } |
6、删除数据
| +(void)deletePeopleByID:(int)ID | |
| 2 | { |
| 3 | if(!db){ |
| 4 | [selfcreatDatabase]; |
| 5 | } |
| 6 | |
| 7 | if(![dbopen]){ |
| 8 | NSLog(@"数据库打开失败"); |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 |
[dbsetShouldCacheStatemen |
| 13 | |
| 14 | //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return |
| 15 | if(![dbtableExists:@"people"]) |
| 16 | { |
| 17 | return; |
| 18 | } |
| 19 | //删除操作 |
| 20 |
[dbexecuteUpdate:@"deletefrom
people where people_id = ?",
[NSStringstringWithFormat |
| 21 | |
| 22 | [dbclose]; |
| 23 | } |
7、修改操作与增加操作的步骤一致
| +(NSArray*)getAllPeople | |
| 2 | { |
| 3 | |
| 4 | if(!db){ |
| 5 | [selfcreatDatabase]; |
| 6 | } |
| 7 | |
| 8 | if(![dbopen]){ |
| 9 | NSLog(@"数据库打开失败"); |
| 10 | returnnil; |
| 11 | } |
| 12 | |
| 13 |
[dbsetShouldCacheStatemen |
| 14 | |
| 15 | if(![dbtableExists:@"people"]) |
| 16 | { |
| 17 | returnnil; |
| 18 | } |
| 19 | |
| 20 | //定义一个可变数组,用来存放查询的结果,返回给调用者 |
| 21 | NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0]; |
| 22 | //定义一个结果集,存放查询的数据 |
| 23 | FMResultSet*rs=[dbexecuteQuery:@"select* from people"]; |
| 24 | //判断结果集中是否有数据,如果有则取出数据 |
| 25 | while([rsnext]){ |
| 26 | People*aPeople=[[Peoplealloc]init]; |
| 27 | |
| 28 | aPeople.peopleID=[rsintForColumn:@"people_id"]; |
| 29 | aPeople.name =[rsstringForColumn:@"name"]; |
| 30 | aPeople.age =[rsintForColumn:@"age"]; |
| 31 | //将查询到的数据放入数组中。 |
| 32 | [peopleArrayaddObject:aPeople]; |
| 33 | } |
| 34 | return[peopleArrayautorelease]; |
| 35 | } |
8、查询
| +(NSArray*)getAllPeople | |
| 2 | { |
| 3 | |
| 4 | if(!db){ |
| 5 | [selfcreatDatabase]; |
| 6 | } |
| 7 | |
| 8 | if(![dbopen]){ |
| 9 | NSLog(@"数据库打开失败"); |
| 10 | returnnil; |
| 11 | } |
| 12 | |
| 13 |
[dbsetShouldCacheStatemen |
| 14 | |
| 15 | if(![dbtableExists:@"people"]) |
| 16 | { |
| 17 | returnnil; |
| 18 | } |
| 19 | |
| 20 | //定义一个可变数组,用来存放查询的结果,返回给调用者 |
| 21 | NSMutableArray*peopleArray=[[NSMutableArrayalloc]initWithArray:0]; |
| 22 | //定义一个结果集,存放查询的数据 |
| 23 | FMResultSet*rs=[dbexecuteQuery:@"select* from people"]; |
| 24 | //判断结果集中是否有数据,如果有则取出数据 |
| 25 | while([rsnext]){ |
| 26 | People*aPeople=[[Peoplealloc]init]; |
| 27 | |
| 28 | aPeople.peopleID=[rsintForColumn:@"people_id"]; |
| 29 | aPeople.name =[rsstringForColumn:@"name"]; |
| 30 | aPeople.age =[rsintForColumn:@"age"]; |
| 31 | //将查询到的数据放入数组中。 |
| 32 | [peopleArrayaddObject:aPeople]; |
| 33 | } |
| 34 | return[peopleArrayautorelease]; |
| 35 | } |
原文地址:http://blog.csdn.net/wangzhaobin/article/details/44943243