1、首先要先導入第三方類庫FMdatabase。
2、獲得存放資料庫檔案的沙盒位址。
View Row Code
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、建立資料庫的操作
View Row Code
1 | +(void)creatDatabase |
2 | { |
3 | db =[[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]]retain]; |
4 | } |
4、建立表
View Row Code
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、增加表資料
View Row Code
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、删除資料
View Row Code
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、查詢
View Row Code
1 | +(NSArray*)getAllPeople |
2 | { |
3 | |
4 | if (!db) { |
5 | [selfcreatDatabase]; |
6 | } |
7 | |
8 | if (![db open]) { |
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 ([rs next]) { |
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 | [peopleArray addObject:aPeople]; |
33 | } |
34 | return[peopleArray autorelease]; |
35 | } |
作者: lzz900201