.h
//
// DBOperation.h
// 2DataBaseSecond
//
// Created by Cecilia on 14-10-16.
// Copyright (c) 2014年 Cecilia. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FMDatabase.h"
#import "ContactssModel.h"
@interface DBOperation : NSObject{
FMDatabase *_db;
}
//单例
-(void)createTable;
+(DBOperation *)shareDBOperation;
//插入
-(void)insertDataWithModel:(ContactssModel *)um;
//删除
-(void)deleteDataWithModel:(ContactssModel *)um;
//修改:update
-(void)updateDataWithModel:(ContactssModel *)um;
//查询是否有某条数据
-(BOOL)isExistWithModel:(ContactssModel *)um;
// 查询某一类数据 Patient Consult Friends
-(NSMutableArray *)FindDataFromdbWithType:(NSString *)TypeStr;
//仅仅插入数据 如果有 不修改
-(void)OnlyinserDataWitModel:(ContactssModel *)model;
//查询所有
-(NSMutableArray *)selectAllData;
//查询某一条数据
-(ContactssModel *)getDataFormodel:(NSString *)ID;
// 返回一类数据 比如患者 或者好友
-(NSMutableArray * )FindDataFromdbWithType:(NSString *)TypeStr;
@end
.m
//
// DBOperation.m
// 2DataBaseSecond
//
// Created by Cecilia on 14-10-16.
// Copyright (c) 2014年 Cecilia. All rights reserved.
//
#import "DBOperation.h"
#import "FMDatabase.h"
#import "ContactssModel.h"
#import <CommonCrypto/CommonDigest.h>
@implementation DBOperation
//非标准单例
+(DBOperation *)shareDBOperation{
static DBOperation *db = nil;
@synchronized(self){ //同步
if (db == nil) {
db = [[self alloc]init];
}
return db;
}
}
// 删除 某一个表
-(void)deletetableFor:(NSString * )tablename{
NSString *sttr =[NSString stringWithFormat:@"DROP TABLE %@",tablename];
[_db executeUpdate:sttr];
}
//初始化
-(id)init{
if (self == [super init]) {
//创建数据库
NSString *paths =NSHomeDirectory();
NSString *loginInfo =@"zhege difang shi jiashuju ";
//[[NSUserDefaults standardUserDefaults] valueForKey:@"Uuid"];
const char *cStr = [loginInfo UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
NSString* MD5 = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]];
paths =[NSString stringWithFormat:@"%@/Documents/%@.db",paths,MD5];
NSLog(@"path = %@",paths);
_db = [[FMDatabase alloc]initWithPath:paths];
BOOL ret = [_db open];
if (ret) {
//打开数据库成功 -- > 创建表
[self createTable];
}
}
return self;
}
#pragma mark - 数据库操作
//1.创建表 user 表
-(void)createTable{/*
Uuid; // 用户的uuid
@property (strong ,nonatomic) NSString * mobile; // 用户的电话号码
@property (strong ,nonatomic) NSString * realeName; // 用户的 名字
@property (strong ,nonatomic) NSString * photo; // x用户的头像
@property (strong ,nonatomic) NSString * Type; //
*/
NSString *str = @"create table if not exists user(uuid,mobile,realeName,photo,Type)";
BOOL ret = [_db executeUpdate:str];
if (ret) {
NSLog(@"表创建成功");
}
else{
NSError *error = _db.lastError;
NSLog(@"表创建失败,%@",error);
}
}
//增加 修改
-(void)insertDataWithModel:(ContactssModel *)um{
// 判断当前数据库中是否已经有该条数据
BOOL isHas = [self isExistWithModel:um];
//如果没有 --> 执行插入操作
if (!isHas) {
NSString *str = @"insert into user values(?,?,?,?,?)";
BOOL ret = [_db executeUpdate:str,um.Uuid,um.mobile,um.realeName,um.photo,um.Type];
if (ret) {
NSLog(@"插入成功");
}
else{
NSLog(@"插入失败");
}
}
//如果有 --> 执行修改操作
else{
[self updateDataWithModel:um];
}
}
// 只增加
-(void)OnlyinserDataWitModel:(ContactssModel *)model{
// 判断当前数据库中是否已经有该条数据
BOOL isHas = [self isExistWithModel:model];
//如果没有 --> 执行插入操作
if (!isHas) {
NSString *str = @"insert into user values(?,?,?,?,?)";
BOOL ret = [_db executeUpdate:str,model.Uuid,model.mobile,model.realeName,model.photo,model.Type];
if (ret) {
NSLog(@"插入成功");
}
else{
NSLog(@"插入失败");
}
}
}
//删除
-(void)deleteDataWithModel:(ContactssModel *)um{
// NSString *str = @"delete from user where ID=?";
// BOOL ret = [_db executeUpdate:str,um.ID];
// if (ret) {
// NSLog(@"删除成功");
// }
// else{
//
// }
}
//修改:update
-(void)updateDataWithModel:(ContactssModel *)um{
// 修改数据uuid,mobile,realeName,photo,Type
NSString *str = @"update user set mobile=?,realeName=?,photo=?,Type=? where uuid=? ";
BOOL ret = [_db executeUpdate:str,um.mobile ,um.realeName,um.photo,um.Type ,um.Uuid];
if (ret) {
NSLog(@"修改成功");
}
else{
NSLog(@"修改失败");
}
//
}
//查询:select
//1.查询是否有某条数据
-(BOOL)isExistWithModel:(ContactssModel *)um{
um.Uuid =[um.Uuid stringByReplacingOccurrencesOfString:@"aaf98f89516bf50b015174e9d5ea1110#" withString:@""];
um.Uuid =[NSString stringWithFormat:@"aaf98f89516bf50b015174e9d5ea1110#%@",um.Uuid];
NSString *str = @"select *from user where uuid=?";
//FMResultSet:查询结果集合
FMResultSet *set = [_db executeQuery:str,um.Uuid];
//[set next] --> 如果有进行一条,进入到循环里,否则,不进循环
if ([set next]) {
return YES;
}
else{
return NO;
}
}
-(NSMutableArray *)FindDataFromdbWithType:(NSString *)TypeStr{
NSMutableArray * dataArrAy =[[NSMutableArray alloc]init];
NSString *str = @"select *from user where Type=?";
//FMResultSet:查询结果集合
FMResultSet *set = [_db executeQuery:str,TypeStr];
//[set next] --> 如果有进行下一条,进入到循环里,否则,不进循环
if ([set next]) {
ContactssModel * model =[[ContactssModel alloc]init];
int numbercolumn = 0;
model.Uuid =[set objectForColumnIndex:numbercolumn];numbercolumn++;
model.mobile =[set objectForColumnIndex:numbercolumn];numbercolumn++;
model.realeName =[set objectForColumnIndex:numbercolumn];numbercolumn++;
model.photo =[set objectForColumnIndex:numbercolumn];numbercolumn++;
model.Type =[set objectForColumnIndex:numbercolumn];numbercolumn ++;
[dataArrAy addObject:model];
}else{
}
return dataArrAy;
}
//获取 某一条数据
-(ContactssModel *)getDataFormodel:(NSString *)ID{
ID =[ID stringByReplacingOccurrencesOfString:@"aaf98f89516bf50b015174e9d5ea1110#" withString:@""];
ID =[NSString stringWithFormat:@"aaf98f89516bf50b015174e9d5ea1110#%@",ID];
ContactssModel * Friend =[[ContactssModel alloc]init];
NSString *str = @"select *from user where uuid=?";
//FMResultSet:查询结果集合
FMResultSet *set = [_db executeQuery:str,ID];
int numberummInde = 0;
if([set next]){
Friend.Uuid =[set objectForColumnIndex:numberummInde];numberummInde++;
Friend.mobile =[set objectForColumnIndex:numberummInde];numberummInde++;
Friend.realeName =[set objectForColumnIndex:numberummInde];numberummInde++;
Friend.photo =[set objectForColumnIndex:numberummInde];numberummInde++;
Friend.Type =[set objectForColumnIndex:numberummInde];numberummInde++;
numberummInde =0;
}
return Friend;
}
//2.查询所有 获取所有数据
-(NSMutableArray *)selectAllData{
NSMutableArray *arr = [[NSMutableArray alloc]init];
//
// NSString *str = @"select *from user";
// FMResultSet *set = [_db executeQuery:str];
// while ([set next]) {
// [arr addObject:Friend];
// }
return arr;
}
@end
//模型
#import <Foundation/Foundation.h>
@interface ContactssModel : NSObject
// uuid mobile realName photo
@property (strong ,nonatomic) NSString * Uuid; // 用户的uuid
@property (strong ,nonatomic) NSString * mobile; // 用户的电话号码
@property (strong ,nonatomic) NSString * realeName; // 用户的 名字
@property (strong ,nonatomic) NSString * photo; // 用户的头像
@property (strong ,nonatomic) NSString * Type; // 用户的类型 患者 医生
+(id)MakeModelWithDic:(NSDictionary *)Dic;
@end
#import "ContactssModel.h"
@implementation ContactssModel
+(id)MakeModelWithDic:(NSDictionary *)Dic{
/*
@property(strong,nonatomic)NSString * Uuid;
@property(strong,nonatomic)NSString * mobile;
@property(strong,nonatomic)NSString * realeName;
@property(strong,nonatomic)NSString * photo;
*/
// static ContactssModel * demodel;
// static dispatch_once_t demodelClassonce;
// dispatch_once(&demodelClassonce, ^{
// demodel =[[ContactssModel alloc]init];
//
// });
ContactssModel * model =[[ContactssModel alloc]init];
model.Uuid =[NSString stringWithFormat:@"aaf98f89516bf50b015174e9d5ea1110#%@",[Dic objectForKey:@"uuid"]];
model.Uuid =[model.Uuid stringByReplacingOccurrencesOfString:@"aaf98f89516bf50b015174e9d5ea1110#" withString:@""];
model.Uuid =[NSString stringWithFormat:@"aaf98f89516bf50b015174e9d5ea1110#%@",model.Uuid];
model.mobile =[NSString stringWithFormat:@"%@",[Dic objectForKey:@"mobile"]];
model.realeName =[NSString stringWithFormat:@"%@",[Dic objectForKey:@"realName"]];
model.photo =[NSString stringWithFormat:@"%@",[Dic objectForKey:@"photo"]];
return model;
}
@end