[摘要]本文介绍iOS 使用FMDB进行数据库操作,并提供详细的示例代码供参考。
1、首先要先导入第三方类库FMdatabase。
2、获得存放数据库文件的沙盒地址。
+(NSString*)databaseFilePath | |
{ | |
NSArray*filePath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); | |
NSString*documentPath=[filePathobjectAtIndex:0]; | |
NSLog(@"%@",filePath); | |
NSString*dbFilePath=[documentPathstringByAppendingPathComponent:@"db.sqlite"]; | |
return dbFilePath; | |
} |
3、创建数据库的操作
+(void)creatDatabase | |
{ | |
db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]]retain]; | |
} |
4、创建表
+(void)creatTable | |
{ | |
//先判断数据库是否存在,如果不存在,创建数据库 | |
if (!db) { | |
[selfcreatDatabase]; | |
} | |
//判断数据库是否已经打开,如果没有打开,提示失败 | |
if (![dbopen]) { | |
NSLog(@"数据库打开失败"); | |
return; | |
} | |
//为数据库设置缓存,提高查询效率 | |
[dbsetShouldCacheStatements:YES]; | |
//判断数据库中是否已经存在这个表,如果不存在则创建该表 | |
if(![dbtableExists:@"people"]) | |
{ | |
[dbexecuteUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "]; | |
NSLog(@"创建完成"); | |
} | |
} |
5、增加表数据
+(void)insertPeople:(People*)aPeople | |
{ | |
if (!db) { | |
[selfcreatDatabase]; | |
} | |
if (![dbopen]) { | |
NSLog(@"数据库打开失败"); | |
return; | |
} | |
[dbsetShouldCacheStatements:YES]; | |
if(![dbtableExists:@"people"]) | |
{ | |
[selfcreatTable]; | |
} | |
//以上操作与创建表是做的判断逻辑相同 | |
//现在表中查询有没有相同的元素,如果有,做修改操作 | |
FMResultSet*rs=[dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; | |
if([rsnext]) | |
{ | |
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]]; | |
} | |
} |
6、删除数据
1 | +(void)deletePeopleByID:(int)ID |
2 | { |
3 | if (!db) { |
4 | [selfcreatDatabase]; |
5 | } |
6 | |
7 | if (![dbopen]) { |
8 | NSLog(@"数据库打开失败"); |
9 | return; |
10 | } |
11 | |
12 | [dbsetShouldCacheStatements:YES]; |
13 | |
14 | //判断表中是否有指定的数据, 如果没有则无删除的必要,直接return |
15 | if(![dbtableExists:@"people"]) |
16 | { |
17 | return; |
18 | } |
19 | //删除操作 |
20 | [dbexecuteUpdate:@"delete from people where people_id = ?",[NSStringstringWithFormat:@"%d",ID]]; |
21 | |
22 | [dbclose]; |
23 | } |
7、修改操作与增加操作的步骤一致
8、查询
1 | +(NSArray*)getAllPeople |
2 | { |
3 | |
4 | if (!db) { |
5 | [selfcreatDatabase]; |
6 | } |
7 | |
8 | if (![dbopen]) { |
9 | NSLog(@"数据库打开失败"); |
10 | returnnil; |
11 | } |
12 | |
13 | [dbsetShouldCacheStatements:YES]; |
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 = [rs intForColumn:@"people_id"]; |
29 | aPeople.name = [rs stringForColumn:@"name"]; |
30 | aPeople.age = [rs intForColumn:@"age"]; |
31 | //将查询到的数据放入数组中。 |
32 | [peopleArrayaddObject:aPeople]; |
33 | } |
34 | return[peopleArrayautorelease]; |
35 | } |
相关文章:
- iOS NSOperation使用方法简介2012-11-04
- iOS 使用NSKeyedArchiver类进行对象序列化和反序列化简介2012-10-30
- iOS TableView的用法介绍2012-10-29
- iOS UILabel控件简介2012-10-29
- iOS SQLite3使用简介2012-10-29
- iOS文件处理介绍(二) 通过plist文件存取文件2012-10-28