iOS 使用FMDB進行數據庫操作

iOS 使用FMDB進行數據庫操作

2012-11-06 19:48 來源:博客園 作者:lzz900201 字號:T|T

[摘要]本文介紹iOS 使用FMDB進行數據庫操作,並提供詳細的示例代碼供參考。

1、首先要先導入第三方類庫FMdatabase。

2、獲得存放數據庫文件的沙盒地址。

1 +(NSString *)databaseFilePath
2 {
3
4 NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
5 NSString *documentPath = [filePath objectAtIndex:0];
6 NSLog(@"%@",filePath);
7 NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"db.sqlite"];
8 return dbFilePath;
9
10 }

3、創建數據庫的操作

1 +(void)creatDatabase
2 {
3 db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain];
4 }

4、創建表

1 +(void)creatTable
2 {
3 //先判斷數據庫是否存在,如果不存在,創建數據庫
4 if (!db) {
5 [selfcreatDatabase];
6 }
7 //判斷數據庫是否已經打開,如果沒有打開,提示失敗
8 if (![db open]) {
9 NSLog(@"數據庫打開失敗");
10 return;
11 }
12
13 //爲數據庫設置緩存,提高查詢效率
14 [dbsetShouldCacheStatements:YES];
15
16 //判斷數據庫中是否已經存在這個表,如果不存在則創建該表
17 if(![dbtableExists:@"people"])
18 {
19 [db executeUpdate:@"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) "];
20
21
22 NSLog(@"創建完成");
23 }
24
25 }

5、增加表數據

1 +(void)insertPeople:(People *)aPeople
2 {
3 if (!db) {
4 [selfcreatDatabase];
5 }
6
7 if (![db open]) {
8 NSLog(@"數據庫打開失敗");
9 return;
10 }
11
12 [dbsetShouldCacheStatements:YES];
13
14 if(![dbtableExists:@"people"])
15 {
16 [selfcreatTable];
17 }
18 //以上操作與創建表是做的判斷邏輯相同
19 //現在表中查詢有沒有相同的元素,如果有,做修改操作
20 FMResultSet *rs = [dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]];
21 if([rs next])
22 {
23 NSLog(@"dddddslsdkien");
24 [dbexecuteUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]];
25 }
26 //向數據庫中插入一條數據
27 else{
28 [dbexecuteUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]];
29 }
30
31 }

6、刪除數據

1 +(void)deletePeopleByID:(int)ID
2 {
3 if (!db) {
4 [selfcreatDatabase];
5 }
6
7 if (![db open]) {
8 NSLog(@"數據庫打開失敗");
9 return;
10 }
11
12 [dbsetShouldCacheStatements:YES];
13
14 //判斷表中是否有指定的數據, 如果沒有則無刪除的必要,直接return
15 if(![dbtableExists:@"people"])
16 {
17 return;
18 }
19 //刪除操作
20 [db executeUpdate:@"delete from people where people_id = ?", [NSStringstringWithFormat:@"%d",ID]];
21
22 [db close];
23 }

7、修改操作與增加操作的步驟一致

8、查詢

1 +(NSArray *)getAllPeople
2 {
3
4 if (!db) {
5 [selfcreatDatabase];
6 }
7
8 if (![db open]) {
9 NSLog(@"數據庫打開失敗");
10 return nil;
11 }
12
13 [dbsetShouldCacheStatements:YES];
14
15 if(![dbtableExists:@"people"])
16 {
17 return nil;
18 }
19
20 //定義一個可變數組,用來存放查詢的結果,返回給調用者
21 NSMutableArray *peopleArray = [[NSMutableArrayalloc] initWithArray:0];
22 //定義一個結果集,存放查詢的數據
23 FMResultSet *rs = [dbexecuteQuery:@"select * from people"];
24 //判斷結果集中是否有數據,如果有則取出數據
25 while ([rs next]) {
26 People *aPeople = [[People alloc] init];
27
28 aPeople.peopleID = [rs intForColumn:@"people_id"];
29 aPeople.name = [rs stringForColumn:@"name"];
30 aPeople.age = [rs intForColumn:@"age"];
31 //將查詢到的數據放入數組中。
32 [peopleArray addObject:aPeople];
33 }
34 return [peopleArray autorelease];
35 }
發佈了34 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章