https://github.com/ccgus/fmdb

在iOS中,需要访问数据库,则需要使用苹果提供的libsqlite3框架,这套框架是苹果基于C语言封装的。而fmdb是开源的第三方提供的数据库访问工具,使用OC对iOS的libsqlite3再次封装,提供了更方便的API使用。
FMDB的核心类为以下三种:
FMDatabase:一个FMDatabase对象代表一个单独的SQLite数据库,通过SQLite语句执行数据库的增删改查操作FMResultSet:使用FMDatabase对象查询数据库后的结果集
FMDatabaseQueue:用于多线程操作数据库,它保证线程安全

FMDB使用

  1. 使用FMDBbase类创建数据库

    1. //1.创建database路径
    2. NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    3. NSString *dbPath = [docuPath stringByAppendingPathComponent:@"test.db"];
    4. NSLog(@"!!!dbPath = %@",dbPath);
    5. //2.创建对应路径下数据库
    6. db = [FMDatabase databaseWithPath:dbPath];
    7. //3.在数据库中进行增删改查操作时,需要判断数据库是否open,如果open失败,可能是权限或者资源不足,数据库操作完成通常使用close关闭数据库
    8. [db open];
    9. if (![db open]) {
    10. NSLog(@"db open fail");
    11. return;
    12. }
    13. //4.数据库中创建表(可创建多张)
    14. NSString *sql = @"create table if not exists t_student ('ID' INTEGER PRIMARY KEY AUTOINCREMENT,'name' TEXT NOT NULL, 'phone' TEXT NOT NULL,'score' INTEGER NOT NULL)";
    15. //5.执行更新操作 此处database直接操作,不考虑多线程问题,多线程问题,用FMDatabaseQueue 每次数据库操作之后都会返回bool数值,YES,表示success,NO,表示fail,可以通过 @see lastError @see lastErrorCode @see lastErrorMessage
    16. BOOL result = [db executeUpdate:sql];
    17. if (result) {
    18. NSLog(@"create table success");
    19. }
    20. [db close];
  2. 插入命令sql语句

    1. [db open];
    2. /*增删改查中 除了查询(executeQuery),其余操作都用(executeUpdate)
    3. //1.sql语句中跟columnname 绑定的value 用 ?表示,不加‘’,可选参数是对象类型如:NSString,不是基本数据结构类型如:int,方法自动匹配对象类型
    4. - (BOOL)executeUpdate:(NSString*)sql, ...;
    5. //2.sql语句中跟columnname 绑定的value 用%@/%d表示,不加‘’
    6. - (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    7. //3.sql语句中跟columnname 绑定的value 用 ?表示的地方依次用 (NSArray *)arguments 对应的数据替代
    8. - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
    9. //4.同3 ,区别在于多一个error指针,记录更新失败
    10. - (BOOL)executeUpdate:(NSString*)sql values:(NSArray * _Nullable)values error:(NSError * _Nullable __autoreleasing *)error;
    11. //5.同3,区别在于用 ? 表示的地方依次用(NSDictionary *)arguments中对应的数据替代
    12. - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
    13. - (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;*/
    14. BOOL result = [db executeUpdate:@"insert into 't_student'(ID,name,phone,score) values(?,?,?,?)" withArgumentsInArray:@[@113,@"x3",@"13",@53]];
    15. if (result) {
    16. NSLog(@"insert into 't_studet' success");
    17. [self showAlertWithTitle:@"insert success" message:nil person:nil];
    18. } else {
    19. [self showAlertWithTitle:[db lastError].description message:nil person:nil];
    20. }
    21. [db close];
  3. 删除命令sql语句

    1. [db open];
    2. ///0.直接sql语句
    3. // BOOL result = [db executeUpdate:@"delete from 't_student' where ID = 110"];
    4. //1.
    5. // BOOL result = [db executeUpdate:@"delete from 't_student' where ID = ?",@(111)];
    6. //2.
    7. // BOOL result = [db executeUpdateWithFormat:@"delete from 't_student' where ID = %d",112];
    8. //3.
    9. BOOL result = [db executeUpdate:@"delete from 't_student' where ID = ?" withArgumentsInArray:@[@113]];
    10. if (result) {
    11. NSLog(@"delete from 't_student' success");
    12. [self showAlertWithTitle:@"delete success" message:nil person:nil];
    13. } else {
    14. [self showAlertWithTitle:[db lastError].description message:nil person:nil];
    15. }
    16. [db close];
  4. 更新命令sql语句

    1. [db open];
    2. //0.直接sql语句
    3. // BOOL result = [db executeUpdate:@"update 't_student' set ID = 110 where name = 'x1'"];
    4. //1.
    5. // BOOL result = [db executeUpdate:@"update 't_student' set ID = ? where name = ?",@111,@"x2" ];
    6. //2.
    7. // BOOL result = [db executeUpdateWithFormat:@"update 't_student' set ID = %d where name = %@",113,@"x3" ];
    8. //3.
    9. BOOL result = [db executeUpdate:@"update 't_student' set ID = ? where name = ?" withArgumentsInArray:@[@113,@"x3"]];
    10. if (result) {
    11. NSLog(@"update 't_student' success");
    12. [self showAlertWithTitle:@"update success" message:nil person:nil];
    13. } else {
    14. [self showAlertWithTitle:[db lastError].description message:nil person:nil];
    15. }
    16. [db close];
  5. 查询命令sql语句

    1. /**
    2. FMResultSet根据column name获取对应数据的方法
    3. intForColumn:
    4. longForColumn:
    5. longLongIntForColumn:
    6. boolForColumn:
    7. doubleForColumn:
    8. stringForColumn:
    9. dataForColumn:
    10. dataNoCopyForColumn:
    11. UTF8StringForColumnIndex:
    12. objectForColumn:
    13. */
    14. [db open];
    15. //0.直接sql语句
    16. // FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = 110"];
    17. //1.
    18. // FMResultSet *result = [db executeQuery:@"select *from 't_student' where ID = ?",@111];
    19. //2.
    20. // FMResultSet *result = [db executeQueryWithFormat:@"select * from 't_student' where ID = %d",112];
    21. //3.
    22. FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = ?" withArgumentsInArray:@[@113]];
    23. //4
    24. // FMResultSet *result = [db executeQuery:@"select * from 't_sutdent' where ID = ?" withParameterDictionary:@{@"ID":@114}];
    25. NSMutableArray *arr = [NSMutableArray array];
    26. while ([result next]) {
    27. PersonVO *person = [PersonVO new];
    28. person.ID = [result intForColumn:@"ID"];
    29. person.name = [result stringForColumn:@"name"];
    30. person.phone = [result stringForColumn:@"phone"];
    31. person.score = [result intForColumn:@"score"];
    32. [arr addObject:person];
    33. NSLog(@"从数据库查询到的人员 %@",person.name);
    34. [self showAlertWithTitle:@"query success" message:nil person:person];
    35. }