频繁的网络请求会给用户不好的体验,在最近开发的一个项目中有一个获取个人详细信息的界面,由于是子页面,进入页面后需要重新加载数据并刷新页面,而,每一次请求服务器再返回数据不仅用户体验不好,也花费手机流量,所以尝试使用一下FMDB。简单看了下别人的博客和官方DEMO,自己尝试将FMDB加入到项目里面去。
创建DB:
-(NSString *)dbPath { static NSString *_path; if (_path) { return _path; }
//获取程序路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; NSString *dbPath = [documentDirectory stringByAppendingString:@"MSProduct.db"]; _path = dbPath; return _path; }
NSString *dbPath = [self dbPath];
//根据路径创建一个数据库对象 FMDatabase *db = [FMDatabase databaseWithPath:dbPath]; return db;
创建Table:
NSString SQL = @"CREATE TABLE IF NOT EXISTS tableName (uid integer,name text,photo text)"; FMDatabase *db = [self createDB]; if ([db open]) { return [db executeUpdate:SQL]; }
字段类型,text,integer,blob,当然其他的我也不清楚,也没去研究。项目里面目前用到了text和integer。 db的executeUpdate方法就是执行SQL语句,其实不只创建Table用到这个方法,删除数据,添加,修改等都可以使用这个方法,查看FMDB源代码可以看到还有其他“重载”方法
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments; - (BOOL)executeUpdate:(NSString*)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error; - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments; - (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
其实只要掌握了SQL语句的使用,那么掌握FMDB的基本功能还不算太难。像删除语句 :DELETE FROM TABLE WHERE 1=1 修改语句 :UPDATE TABLE SET NAME = 'newname' WHERE 1=1
最后还有一个查询,查询结果都放在 FMResultSet 中,它有一个 next 方法,循环遍历每一行(row)数据。示例代码:
NSString *querySQL =[NSString stringWithFormat:@"SELECT * FROM %@ WHERE cvnumber=%ld",kMSTableNameUserInfo,cvnumber]; FMResultSet *result = [[MSDataBase sharedMSDataBase] queryData:querySQL]; //FMResultSet *result = [db executeQuery:SQL];
MSUserCard *user = [[MSUserCard alloc] init]; while ([result next]) { user.cvnumber = [result intForColumn:@"cvnumber"]; user.name = [result stringForColumn:@"name"]; user.headphoto = [result stringForColumn:@"headphotos"]; user.company = [result stringForColumn:@"company"]; user.email = [result stringForColumn:@"email"]; user.mobile = [result stringForColumn:@"mobile"]; user.position = [result stringForColumn:@"position"]; user.landline = [result stringForColumn:@"landline"]; user.weixin = [result stringForColumn:@"weixin"]; user.address = [result stringForColumn:@"address"]; user.ability = [result stringForColumn:@"ability"]; user.achieve = [result stringForColumn:@"achieve"]; user.identitytitle = [result stringForColumn:@"identitytitle"]; user.hidemobile = [result intForColumn:@"hidemobile"]; }
上述例子是获取单条数据的。当然多条同样适用,只要读取出来,剩下的随便操作了。FMResultSet其他方法
- (int)columnCount; - (int)columnIndexForName:(NSString*)columnName; - (NSString*)columnNameForIndex:(int)columnIdx; - (int)intForColumn:(NSString*)columnName; - (int)intForColumnIndex:(int)columnIdx; - (long)longForColumn:(NSString*)columnName; - (long)longForColumnIndex:(int)columnIdx; - (long long int)longLongIntForColumn:(NSString*)columnName; - (long long int)longLongIntForColumnIndex:(int)columnIdx; - (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName; - (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx; - (BOOL)boolForColumn:(NSString*)columnName; - (BOOL)boolForColumnIndex:(int)columnIdx; - (double)doubleForColumn:(NSString*)columnName; - (double)doubleForColumnIndex:(int)columnIdx; - (NSString*)stringForColumn:(NSString*)columnName; - (NSString*)stringForColumnIndex:(int)columnIdx; - (NSDate*)dateForColumn:(NSString*)columnName; - (NSDate*)dateForColumnIndex:(int)columnIdx; - (NSData*)dataForColumn:(NSString*)columnName; - (NSData*)dataForColumnIndex:(int)columnIdx; - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName; - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx; - (id)objectForColumnName:(NSString*)columnName; - (id)objectForColumnIndex:(int)columnIdx;
最后,将FMDB应用到我开头提到的例子上,读取数据先从本地数据库读取,如果有数据,就使用。如果没有就访问网络获取数据,然后将数据保存到本地的数据库表中。下次进来的时候就不会再请求网络数据了。那么什么时候更新本地数据库就看具体的业务需求了。