[摘要]本文介紹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