dao类
方法申明
//创建people表
+(BOOL)createTablefriend;
//向people表插入一条数据 ,方法带参数,需要将人的名字和年龄传过来
+(BOOL)insertIntofriendWithDic:(NSDictionary *)dictionary;
//查询最大的id号
+(int)selectMaxID;
//查询表中所有的人
+(NSMutableArray*)selectAllfriend;
//根据ID或者名字查找某一个人
+(friendModel*)selectFriendWithString:(NSString *)flagString stringTypeIsNameOrId:(BOOL)isName;
//根据用户的id,修改用户的 昵称和头像
+(void)updateFriendWithHeadUrl:(NSString *)headUrl withNick:(NSString *)aNick forFriendId:(NSString*)idString;
//根据用户的id,修改用户的昵称
+(void)updateFriendWithNick:(NSString*)aNick forFriendId:(NSString*)idString;
//根据用户的id,修改用户的备注
+(void)updateFriendWithRemark:(NSString*)aRemark forFriendId:(NSString*)idString;
//根据用户的id,修改用户的 头像
+(void)updateFriendWithHeadUrl:(NSString *)headUrl forFriendId:(NSString*)idString;
//根据id号删除一个人
+(BOOL)deleteForfriendID:(NSInteger)aID;
+(void)deleteAllResource;
方法实现
//创建people表
+(BOOL)createTablefriend
{
//1.获取数据对象
FMDatabase *db= [dataBaseTool shareDatabase];
//2.执行创建表的sql语句
BOOL rs= [db executeUpdate:@”create table if not exists friend(id integer primary key autoincrement,city text,acountry text,province text,t_sign text,nickname text,headimgurl text,phone text,remarks_name text,recomment integer,member_id integer,relationship integer,sex integer,shown_photo integer,is_yanz integer)”];
//3.关闭数据库
[dataBaseTool closeDatabase];
if (rs==YES) {
NSLog(@”创建表成功”);
return YES;
}
return NO;
}
//向people表插入一条数据 ,方法带参数,需要将人的名字和年龄传过来
+(BOOL)insertIntofriendWithDic:(NSDictionary *)dictionary
{
FMDatabase *db =[dataBaseTool shareDatabase];
BOOL rs= [db executeUpdate:@”insert into friend(city,acountry,province,t_sign,nickname,headimgurl,phone,remarks_name,recomment,member_id,relationship,sex,shown_photo,is_yanz)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)”,dictionary[@”city”],dictionary[@”country”],dictionary[@”province”],dictionary[@”t_sign”],dictionary[@”nickname”],dictionary[@”headimgurl”],dictionary[@”phone”],dictionary[@”remarks_name”],dictionary[@”recomment”],dictionary[@”member_id”],dictionary[@”relationship”],dictionary[@”sex”],dictionary[@”shown_photo”],dictionary[@”is_yanz”]];
[dataBaseTool closeDatabase];
if (rs==YES) {
NSLog(@”插入成功”);
return YES;
}
return NO;
}
//查询最大的id号
+(int)selectMaxID
{
FMDatabase *db =[dataBaseTool shareDatabase];
FMResultSet *set =[db executeQuery:@”select max(id) from friend”];
//结果集中肯定只有一条数据,可以不写while循环
[set next];
int max = [set intForColumn:@”max(id)”];
//关闭结果集
[set close];
//关闭数据库
[dataBaseTool closeDatabase];
return max;
}
//查询表中所有的人
+(NSMutableArray*)selectAllfriend
{
//获取数据库对象
FMDatabase *db =[dataBaseTool shareDatabase];
//执行查询的sql语句
FMResultSet *set =[db executeQuery:@”select * from friend”];
NSMutableArray *arr =[NSMutableArray arrayWithCapacity:0];
//从结果集中取数据
while ([set next]) {
// NSString *strr = [dataBaseTool filePath];
NSString *city = [[set stringForColumn:@”city”] length] > 0 ? [set stringForColumn:@”city”] : @””;
NSString *acountry = [[set stringForColumn:@”acountry”] length] > 0 ? [set stringForColumn:@”acountry”] : @””;
NSString *province = [[set stringForColumn:@”province”] length] > 0 ? [set stringForColumn:@”province”] : @””;
NSString *t_sign = [[set stringForColumn:@”t_sign”] length] > 0 ? [set stringForColumn:@”t_sign”] : @””;
NSString *nickname = [[set stringForColumn:@”nickname”] length] > 0 ? [set stringForColumn:@”nickname”] : @””;
NSString *headimgurl = [[set stringForColumn:@”headimgurl”] length] > 0 ? [set stringForColumn:@”headimgurl”] : @””;
NSString *phone = [[set stringForColumn:@”phone”] length] > 0 ? [set stringForColumn:@”phone”] : @””;
NSString *remarks_name = [[set stringForColumn:@”remarks_name”] length] > 0 ? [set stringForColumn:@”remarks_name”] : @””;
NSInteger recomment = [set intForColumn:@”recomment”];
NSInteger member_id = [set intForColumn:@”member_id”];
NSInteger relationship = [set intForColumn:@”relationship”];
NSInteger sex = [set intForColumn:@”sex”];
NSInteger shown_photo = [set intForColumn:@”shown_photo”];
NSInteger is_yanz = [set intForColumn:@”is_yanz”];
NSDictionary *tempDic = @{@”city”:city,@”country”:acountry,@”province”:province,@”t_sign”:t_sign,@”nickname”:nickname,@”headimgurl”:headimgurl,@”phone”:phone,@”remarks_name”:remarks_name,@”recomment”:@(recomment),@”member_id”:@(member_id),@”relationship”:@(relationship),@”sex”:@(sex),@”shown_photo”:@(shown_photo),@”is_yanz”:@(is_yanz)};
friendModel *friend = [[friendModel alloc] initWithDic:tempDic];
friend.fmdbID = [set intForColumn:@”id”];
[arr addObject:friend];
}
//关闭结果集
[set close];
//关闭数据库
[dataBaseTool closeDatabase];
return arr;
}
//根据ID或者名字查找某一个人
+(friendModel*)selectFriendWithString:(NSString *)flagString stringTypeIsNameOrId:(BOOL)isName
{
FMDatabase *db =[dataBaseTool shareDatabase];
FMResultSet *set = nil;
if (isName == YES)
{
set =[db executeQuery:@”select * from friend where nickname= ?”,flagString];
}else
{
set =[db executeQuery:@”select * from friend where member_id= ?”,flagString];
}
if ([set next])
{
NSString *city = [[set stringForColumn:@”city”] length] > 0 ? [set stringForColumn:@”city”] : @””;
NSString *acountry = [[set stringForColumn:@”acountry”] length] > 0 ? [set stringForColumn:@”acountry”] : @””;
NSString *province = [[set stringForColumn:@”province”] length] > 0 ? [set stringForColumn:@”province”] : @””;
NSString *t_sign = [[set stringForColumn:@”t_sign”] length] > 0 ? [set stringForColumn:@”t_sign”] : @””;
NSString *nickname = [[set stringForColumn:@”nickname”] length] > 0 ? [set stringForColumn:@”nickname”] : @””;
NSString *headimgurl = [[set stringForColumn:@”headimgurl”] length] > 0 ? [set stringForColumn:@”headimgurl”] : @””;
NSString *phone = [[set stringForColumn:@”phone”] length] > 0 ? [set stringForColumn:@”phone”] : @””;
NSString *remarks_name = [[set stringForColumn:@”remarks_name”] length] > 0 ? [set stringForColumn:@”remarks_name”] : @””;
NSInteger recomment = [set intForColumn:@”recomment”];
NSInteger member_id = [set intForColumn:@”member_id”];
NSInteger relationship = [set intForColumn:@”relationship”];
NSInteger sex = [set intForColumn:@”sex”];
NSInteger shown_photo = [set intForColumn:@”shown_photo”];
NSInteger is_yanz = [set intForColumn:@”is_yanz”];
NSDictionary *tempDic = @{@”city”:city,@”country”:acountry,@”province”:province,@”t_sign”:t_sign,@”nickname”:nickname,@”headimgurl”:headimgurl,@”phone”:phone,@”remarks_name”:remarks_name,@”recomment”:@(recomment),@”member_id”:@(member_id),@”relationship”:@(relationship),@”sex”:@(sex),@”shown_photo”:@(shown_photo),@”is_yanz”:@(is_yanz)};
friendModel *friend = [[friendModel alloc] initWithDic:tempDic];
friend.fmdbID = [set intForColumn:@”id”];
[set close];
[dataBaseTool closeDatabase];
return friend;
}else
{
[set close];
[dataBaseTool closeDatabase];
return nil;
}
}
//根据用户的id,修改用户的 昵称和头像
+(void)updateFriendWithHeadUrl:(NSString *)headUrl withNick:(NSString *)aNick forFriendId:(NSString*)idString
{
FMDatabase *db =[dataBaseTool shareDatabase];
BOOL firstOk = [db executeUpdate:@”update friend set headimgurl = ? where member_id =?”,headUrl,idString];
BOOL twoOk = [db executeUpdate:@”update friend set nickname = ? where member_id =?”,aNick,idString];
if (firstOk && twoOk)
{
NSLog(@”修改成功”);
}else{
NSLog(@”修改失败”);
}
[dataBaseTool closeDatabase];
}
//根据用户的id,修改用户的昵称
+(void)updateFriendWithNick:(NSString*)aNick forFriendId:(NSString*)idString
{
FMDatabase *db =[dataBaseTool shareDatabase];
[db executeUpdate:@”update friend set nickname = ? where member_id =?”,aNick,idString];
[dataBaseTool closeDatabase];
}
//根据用户的id,修改用户的备注
+(void)updateFriendWithRemark:(NSString*)aRemark forFriendId:(NSString*)idString
{
FMDatabase *db =[dataBaseTool shareDatabase];
[db executeUpdate:@”update friend set remarks_name = ? where member_id =?”,aRemark,idString];
[dataBaseTool closeDatabase];
}
//根据用户的id,修改用户的 头像
+(void)updateFriendWithHeadUrl:(NSString *)headUrl forFriendId:(NSString*)idString
{
FMDatabase *db =[dataBaseTool shareDatabase];
[db executeUpdate:@”update friend set headimgurl = ? where member_id =?”,headUrl,idString];
[dataBaseTool closeDatabase];
}
//根据id号删除一个人
+(BOOL)deleteForfriendID:(NSInteger)aID
{
FMDatabase *db =[dataBaseTool shareDatabase];
BOOL rs= [db executeUpdate:@”delete from friend where member_id=?”,@(aID)];
[dataBaseTool closeDatabase];
if (rs==YES) {
NSLog(@”删除成功”);
return YES;
}
return NO;
}
+(void)deleteAllResource
{
FMDatabase *db =[dataBaseTool shareDatabase];
[db executeUpdate:@”delete from friend”];
[dataBaseTool closeDatabase];
}
Tool类
申明方法
//获取数据库对象
+(FMDatabase*)shareDatabase;
//打开数据库的方法
+(BOOL)openDatabase;
//关闭数据库的方法
+(BOOL)closeDatabase;
//获取数据库文件路径
+(NSString*)filePath;
实现方法
//写在花括号中的变量是实例变量,类方法中不能使用实例变量
//声明在方法之外的变量 才是全局变量。 全部变量的作用域是我们的整个工程。
//为了全局变量的数据安全性,我们用static修饰全局变量。限定该全局变量只能在当前文件中用
static FMDatabase *_db=nil;
//获取FMDatabase对象,并且保证整个工程中只有这一个对象
+(FMDatabase*)shareDatabase{
//如果_db不存在,就创建一个,如果存在就直接返回
if (_db==nil) {
_db =[[FMDatabase alloc]initWithPath:[self filePath]];
}
//打开数据库 获取对象之后直接就打开,不用再手动调用打开数据库的代码了
[self openDatabase];
//设置缓冲 提供效率
[_db setShouldCacheStatements:YES];
return _db;
}
//打开数据库
+(BOOL)openDatabase{
if (![_db open]) { //打开失败
[_db close];
NSAssert(NO, @”数据库打开失败”);
return NO;
}
return YES;
}
//关闭数据库
+(BOOL)closeDatabase{
if (![_db close]) {
NSAssert(NO, @”数据库关闭失败”);
return NO;
}
return YES;
}
//数据库文件路径
+(NSString*)filePath{
return [NSHomeDirectory() stringByAppendingPathComponent:@”Documents/friend.sqlite”];
}
model类
申明
@property (nonatomic,strong)NSString *city;
@property (nonatomic,strong)NSString *acountry;
@property (nonatomic,strong)NSMutableArray *photos;
@property (nonatomic,strong)NSString *province;
@property (nonatomic,strong)NSString *t_sign;
@property (nonatomic,strong)NSString *nickname;
@property (nonatomic,strong)NSString *headimgurl;
@property (nonatomic,strong)NSString *phone;
@property (nonatomic,strong)NSString *remarks_name;
@property (nonatomic,assign)NSInteger recomment;
@property (nonatomic,assign)NSInteger member_id;
@property (nonatomic,assign)NSInteger relationship;
@property (nonatomic,assign)NSInteger sex;
@property (nonatomic,assign)NSInteger shown_photo;
@property (nonatomic,assign)NSInteger is_yanz;
@property (nonatomic,assign)NSInteger fmdbID;//数据库用
– (instancetype)initWithDic:(NSDictionary *)dic;
实现
– (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
self.city = dic[@”city”];
self.acountry = dic[@”country”];
self.province = dic[@”province”];
self.t_sign = dic[@”t_sign”];
_nickname = dic[@”nickname”];
_headimgurl = dic[@”headimgurl”];
_phone = dic[@”phone”];
_remarks_name = dic[@”remarks_name”];
if ([dic.allKeys containsObject:@”photos”]) {
self.photos = [[NSMutableArray alloc] initWithArray:dic[@”photos”]];
}
_recomment = [dic[@”recomment”] integerValue];
_member_id = [dic[@”member_id”] integerValue];
self.relationship = [dic[@”relationship”] integerValue];
self.sex = [dic[@”sex”] integerValue];
self.shown_photo = [dic[@”shown_photo”] integerValue];
self.is_yanz = [dic[@”is_yanz”] integerValue];
}
return self;
}