FMDB使用介绍

最近公司要开发新项目,要使用到数据库,由于以前使用的较少,所以做下知识补充。
介绍

iOS中原生的SQLite API在使用上不好用,使用时非常不便。于是,就出现了一系列将SQLite API进行封装的代码库,例如FMDB、PlausibleDatabase、SQLitePersistentObjects等。

FMDB的使用

注意事项 :

使用 FMDB 先要导入系统库 libsqlite3.0.dylib

ARC工程添加MRC文件: -fno-objc-arc

MRC工程添加ARC文件: -fobjc-arc

如果一个工程为MRC,其中要添加ARC的文件:

选择target - build phases - compile sources - 单击ARC的文件将compiler flags设置为:-fobjc-arc

如果一个工程为ARC,其中要添加MRC的文件:

同样的路径,然后单击MRC的文件将compiler flags设置为:-fno-objc-arc

在 FMDB 中有三个重要的类:

1.FMDatabase:是一个提供 SQLite 数据库的类,用于执行 SQL 语句。

2.FMResultSet:用在FMDatabase中执行查询的结果的类。

3.FMDatabaseQueue:在多线程下查询和更新数据库用到的类。

数据库创建

FMDatabase是通过一个 SQLite 数据库文件路径创建的,此路径可以是以下三者之一:

1.一个文件的系统路径。磁盘中可以不存在此文件,因为如果不存在会自动为你创建。

2.一个空的字符串@""。会在临时位置创建一个空的数据库,当FMDatabase连接关闭时,该数据库会被删除。

3.NULL。会在内存中创建一个数据库,当FMDatabase连接关闭时,该数据库会被销毁

//引用头文件
#import"FMDatabase.h"
#import"FMDatabaseQueue.h"
//获取数据库文件的路径:
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *path = [doc stringByAppendingPathComponent:@"user.sqlite"]; self.dbPath = path;
// 建表 
- (void)createTable { 
NSLog(@"%s", __func__); 
NSFileManager *fileManager = [NSFileManager defaultManager];
 if ([fileManager fileExistsAtPath:self.dbPath] == NO) { 
// create it 
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; 
if ([db open]) { 
NSString *sql = @"CREATE TABLE 'User' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' VARCHAR(30), 'password' VARCHAR(30))"; 
BOOL res = [db executeUpdate:sql]; if (!res) { NSLog(@"error when creating db table");
 } else { 
NSLog(@"success to creating db table"); 
} 
[db close]; 
} else { NSLog(@"error when open db"); } } 
}
// 插入数据 
- (void)insertData { 
NSLog(@"%s", __func__); static int idx = 1; 
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; 
if ([db open]) { NSString *sql = @"insert into user (name, password) values(?, ?) ";
NSString *name = [NSString stringWithFormat:@"ZL%d", idx++]; 
BOOL res = [db executeUpdate:sql, name, @"girl"]; if (!res) { 
NSLog(@"error to insert data"); 
} else { 
NSLog(@"success to insert data"); 
ZLTestModel *model = [ZLTestModel modelWith:name id:idx];
[self.userArr addObject:model]; 
[self.tableView reloadData];
 } 
[db close]; 
} 
}
// 更新数据
- (void)updateData { NSLog(@"%s", __func__); 
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
 if ([db open]) { 
NSString *sql = @"UPDATE USER SET id = ? WHERE name = ?"; 
BOOL res = [db executeUpdate:sql, @"1", @"zl"]; 
if (!res) { 
NSLog(@"error to UPDATE data"); 
} else { NSLog(@"success to UPDATE data"); [self queryData]; 
}
 [db close]; 
} 
}
// 删除数据 
- (void)deleteData { NSLog(@"%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; 
if ([db open]) { 
NSString *sql = @"delete from user"; BOOL res = [db executeUpdate:sql]; 
if (!res) { 
NSLog(@"error to delete db data"); 
} else { 
NSLog(@"success to delete db data"); 
[self.userArr removeAllObjects]; 
[self.tableView reloadData]; 
} 
[db close]; 
}
}
// 查询数据 
- (void)queryData { 
NSLog(@"%s", __func__);
 FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; 
if ([db open]) { NSString *sql = @"select *from user"; FMResultSet *rs = [db executeQuery:sql];
 while ([rs next]) {
 int userId = [rs intForColumn:@"id"];
NSString *name = [rs stringForColumn:@"name"]; NSString *pass = [rs stringForColumn:@"password"]; NSLog(@"user id = %d, name = %@, pass = %@", userId, name, pass); 
ZLTestModel *model = [ZLTestModel modelWith:name id:userId]; 
[self.userArr addObject:model]; 
[self.tableView reloadData]; } 
[db close];
 }
 }
//multithread
- (void)multithread { 
NSLog(@"%s", __func__); FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath]; 
dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL); 
dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL); 
dispatch_async(q1, ^{ for (int i = 0; i < 100; ++i) { 
[queue inDatabase:^(FMDatabase *db) { 
NSString *sql = @"insert into user (name, password) values(?, ?) "; 
NSString *name = [NSString stringWithFormat:@"queue111 %d", i]; 
BOOL res = [db executeUpdate:sql, name, @"boy"]; 
if (!res) { 
NSLog(@"error to add db data: %@", name); 
} else { 
NSLog(@"success to add db data: %@", name); } }]; } }); dispatch_async(q2, ^{ for (int i = 0; i < 100; ++i) {
[queue inDatabase:^(FMDatabase *db) { 
NSString *sql = @"insert into user (name, password) values(?, ?) "; 
NSString *name = [NSString stringWithFormat:@"queue222 %d", i]; 
BOOL res = [db executeUpdate:sql, name, @"boy"];
 if (!res) { 
NSLog(@"error to add db data: %@", name); } else { NSLog(@"success to add db data: %@", name); } }]; } }); }

你可能感兴趣的:(FMDB使用介绍)