FMDB简单使用
FMDB增删查改 https://github.com/ccgus/fmdb
创建,插入,更新和删除:使用executeUpdate方法,而查询则用executeQuery
1.实例化FMDatabase
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//paths: ios下Document路径,Document为ios中可读写的文件夹NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentDirectory = [paths objectAtIndex:0]; //dbPath: 数据库路径,在Document中。NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"]; //创建数据库实例 db 这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;if (![db open]) {NSLog(@"Could not open db.");return ;} |
2.创建表
|
1
2
3
|
//创建表 NSString *sql = @"create table notes(text string ,time string) "; //1 "AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY" BOOL isSuccess = [_mgr.fmdb executeUpdate:sql]; if (isSuccess) { NSLog(@"Create table success"); } |
3.插入
|
1
2
3
|
//插入数据使用OC中的类型 text对应为NSString integer对应为NSNumber的整形NSString *sql = [NSString stringWithFormat:@"insert into notes(text,time) values ('%@','%@')",note.text,note.time]; if ([self.fmdb executeUpdate:sql]) { NSLog(@"成功!"); }else NSLog(@"失败!"); |
4.更新
|
1
2
3
|
//更新数据[db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"Hello",@"Jeffery"]; |
5.删除
|
1
2
3
|
//删除数据[db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"Jeffery"]; |
6.查询
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-(NSArray *)queryNotes{ FMResultSet* set = [self.fmdb executeQuery:@"select * from notes"]; NSMutableArray *mutableArray = [NSMutableArray array]; while ([set next]) { NSString *text = [set stringForColumn:@"text"]; NSString *time = [set stringForColumn:@"time"];
SZNote *note = [[SZNote alloc]init]; note.time =time; note.text = text;
[mutableArray addObject:note];
} return mutableArray; } |
备注:
需要提一点就是:线程安全
如果我们的app需要多线程操作数据库,那么就需要使用FMDatabaseQueue来保证线程安全了。 切记不能在多个线程中共同一个FMDatabase对象并且在多个线程中同时使用,这个类本身不是线程安全的,这样使用会造成数据混乱等问题。
使用FMDatabaseQueue很简单,首先用一个数据库文件地址来初使化FMDatabaseQueue,然后就可以将一个闭包(block)传入inDatabase方法中。 在闭包中操作数据库,而不直接参与FMDatabase的管理。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 创建,最好放在一个单例的类中FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; // 使用[queue inDatabase:^(FMDatabase *db) { [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; FMResultSet *rs = [db executeQuery:@"select * from foo"]; while ([rs next]) { // … }}]; // 如果要支持事务[queue inTransaction:^(FMDatabase *db, BOOL *rollback) { [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; if (whoopsSomethingWrongHappened) { *rollback = YES; return; } // etc… [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];}]; |
附:
创建data类型用blob
创建表
[db executeUpdate:@"CREATE TABLE PersonList (Name text, Age integer, Sex integer, Phone text, Address text, Photo blob)"];
插入
[db executeUpdate:@"INSERT INTO PersonList (Name, Age, Sex, Phone, Address, Photo) VALUES (?,?,?,?,?,?)", @"Jone", [NSNumber numberWithInt:20], [NSNumber numberWithInt:0], @“091234567”, @“Taiwan, R.O.C”, [NSData dataWithContentsOfFile: filepath]];

浙公网安备 33010602011771号