在iOS中,需要访问数据库,则需要使用苹果提供的libsqlite3框架,这套框架是苹果基于C语言封装的。而fmdb是开源的第三方提供的数据库访问工具,使用OC对iOS的libsqlite3再次封装,提供了更方便的API使用。
FMDB的核心类为以下三种:
FMDatabase:一个FMDatabase对象代表一个单独的SQLite数据库,通过SQLite语句执行数据库的增删改查操作FMResultSet:使用FMDatabase对象查询数据库后的结果集
FMDatabaseQueue:用于多线程操作数据库,它保证线程安全
FMDB使用
使用FMDBbase类创建数据库
//1.创建database路径
NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *dbPath = [docuPath stringByAppendingPathComponent:@"test.db"];
NSLog(@"!!!dbPath = %@",dbPath);
//2.创建对应路径下数据库
db = [FMDatabase databaseWithPath:dbPath];
//3.在数据库中进行增删改查操作时,需要判断数据库是否open,如果open失败,可能是权限或者资源不足,数据库操作完成通常使用close关闭数据库
[db open];
if (![db open]) {
NSLog(@"db open fail");
return;
}
//4.数据库中创建表(可创建多张)
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)";
//5.执行更新操作 此处database直接操作,不考虑多线程问题,多线程问题,用FMDatabaseQueue 每次数据库操作之后都会返回bool数值,YES,表示success,NO,表示fail,可以通过 @see lastError @see lastErrorCode @see lastErrorMessage
BOOL result = [db executeUpdate:sql];
if (result) {
NSLog(@"create table success");
}
[db close];
插入命令sql语句
[db open];
/*增删改查中 除了查询(executeQuery),其余操作都用(executeUpdate)
//1.sql语句中跟columnname 绑定的value 用 ?表示,不加‘’,可选参数是对象类型如:NSString,不是基本数据结构类型如:int,方法自动匹配对象类型
- (BOOL)executeUpdate:(NSString*)sql, ...;
//2.sql语句中跟columnname 绑定的value 用%@/%d表示,不加‘’
- (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
//3.sql语句中跟columnname 绑定的value 用 ?表示的地方依次用 (NSArray *)arguments 对应的数据替代
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
//4.同3 ,区别在于多一个error指针,记录更新失败
- (BOOL)executeUpdate:(NSString*)sql values:(NSArray * _Nullable)values error:(NSError * _Nullable __autoreleasing *)error;
//5.同3,区别在于用 ? 表示的地方依次用(NSDictionary *)arguments中对应的数据替代
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
- (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;*/
BOOL result = [db executeUpdate:@"insert into 't_student'(ID,name,phone,score) values(?,?,?,?)" withArgumentsInArray:@[@113,@"x3",@"13",@53]];
if (result) {
NSLog(@"insert into 't_studet' success");
[self showAlertWithTitle:@"insert success" message:nil person:nil];
} else {
[self showAlertWithTitle:[db lastError].description message:nil person:nil];
}
[db close];
删除命令sql语句
[db open];
///0.直接sql语句
// BOOL result = [db executeUpdate:@"delete from 't_student' where ID = 110"];
//1.
// BOOL result = [db executeUpdate:@"delete from 't_student' where ID = ?",@(111)];
//2.
// BOOL result = [db executeUpdateWithFormat:@"delete from 't_student' where ID = %d",112];
//3.
BOOL result = [db executeUpdate:@"delete from 't_student' where ID = ?" withArgumentsInArray:@[@113]];
if (result) {
NSLog(@"delete from 't_student' success");
[self showAlertWithTitle:@"delete success" message:nil person:nil];
} else {
[self showAlertWithTitle:[db lastError].description message:nil person:nil];
}
[db close];
更新命令sql语句
[db open];
//0.直接sql语句
// BOOL result = [db executeUpdate:@"update 't_student' set ID = 110 where name = 'x1'"];
//1.
// BOOL result = [db executeUpdate:@"update 't_student' set ID = ? where name = ?",@111,@"x2" ];
//2.
// BOOL result = [db executeUpdateWithFormat:@"update 't_student' set ID = %d where name = %@",113,@"x3" ];
//3.
BOOL result = [db executeUpdate:@"update 't_student' set ID = ? where name = ?" withArgumentsInArray:@[@113,@"x3"]];
if (result) {
NSLog(@"update 't_student' success");
[self showAlertWithTitle:@"update success" message:nil person:nil];
} else {
[self showAlertWithTitle:[db lastError].description message:nil person:nil];
}
[db close];
查询命令sql语句
/**
FMResultSet根据column name获取对应数据的方法
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumnIndex:
objectForColumn:
*/
[db open];
//0.直接sql语句
// FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = 110"];
//1.
// FMResultSet *result = [db executeQuery:@"select *from 't_student' where ID = ?",@111];
//2.
// FMResultSet *result = [db executeQueryWithFormat:@"select * from 't_student' where ID = %d",112];
//3.
FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = ?" withArgumentsInArray:@[@113]];
//4
// FMResultSet *result = [db executeQuery:@"select * from 't_sutdent' where ID = ?" withParameterDictionary:@{@"ID":@114}];
NSMutableArray *arr = [NSMutableArray array];
while ([result next]) {
PersonVO *person = [PersonVO new];
person.ID = [result intForColumn:@"ID"];
person.name = [result stringForColumn:@"name"];
person.phone = [result stringForColumn:@"phone"];
person.score = [result intForColumn:@"score"];
[arr addObject:person];
NSLog(@"从数据库查询到的人员 %@",person.name);
[self showAlertWithTitle:@"query success" message:nil person:person];
}