FMDB
2015-08-24 20:15 南峰_ldl 阅读(114) 评论(0) 编辑 收藏 举报FMDB的使用。
+(id)sharedInstance { static AppointmentsDatabase *appointmentsdb = nil; if (appointmentsdb == nil) { appointmentsdb = [[[self class] alloc] init]; } return appointmentsdb; } -(id)init { if (self = [super init]) { [self configDatabase]; } return self; } -(NSArray *)recordWithRecordType { NSString *sql = @"select * from appointmentslist order by start_time desc"; FMResultSet *result = [_database executeQuery:sql]; NSMutableArray *marr = [[NSMutableArray alloc] init]; while ([result next]) { AppointmentModel *model = [[AppointmentModel alloc] init]; model.id = [result stringForColumn:@"lawyer_id"]; model.start_time = (NSString *)[result stringForColumn:@"start_time"]; model.end_time = [result stringForColumn:@"end_time"]; model.available = [result stringForColumn:@"available"]; model.client_id = [result stringForColumn:@"client_id"]; model.status = [result stringForColumn:@"status"]; model.reason = [result stringForColumn:@"reason"]; model.owner = [result stringForColumn:@"owner"]; model.client_owner = [result stringForColumn:@"client_owner"]; [marr addObject:model]; } return marr; } -(void)removeRecordWithDictionary:(NSDictionary *)dictionary { NSString *sql = @"delete from appointmentslist where appointment_id=?"; [_database executeUpdate:sql,dictionary[@"id"]]; } -(BOOL)isExistsRecordWithModel:(AppointmentModel *)model { NSString *sql = @"select * from appointmentslist where appointment_id=?"; FMResultSet *resultSet = [_database executeQuery:sql,model.id]; while ([resultSet next]) { return YES; } return NO; } -(void)updateRecordWithDictionary:(NSMutableDictionary *)mutableDictionary { NSString *sql = @"update appointmentslist set start_time=?,end_time=?,available=?,client_id=?,status=?,reason=? where appointment_id=?"; BOOL isOk = [_database executeUpdate:sql, mutableDictionary[@"start_time"], mutableDictionary[@"end_time"], mutableDictionary[@"available"], mutableDictionary[@"client_id"], mutableDictionary[@"status"], mutableDictionary[@"reason"],mutableDictionary[@"id"] ]; if (!isOk) { NSLog(@"update errer"); } } -(void)addRecordWithModel:(AppointmentModel *)model { NSString *sql = @"insert into appointmentslist(appointment_id,start_time,end_time,available,client_id,status,reason,owner,client_owner) values(?,?,?,?,?,?,?,?,?);"; BOOL b = [_database executeUpdate:sql, model.id, model.start_time, model.end_time, model.available, model.client_id, model.status, model.reason, model.owner, model.client_owner ]; if (!b) { NSLog(@"insert errer"); } } -(void)deleteAllData { NSString *sql = @"delete from appointmentslist;"; BOOL b = [_database executeUpdate:sql]; if (!b) { NSLog(@"delete errer"); } } -(void)configDatabase { NSString *path = [NSString stringWithFormat:@"%@/Documents/referral.sqlite",NSHomeDirectory()]; _database = [[FMDatabase alloc] initWithPath:path]; if (!_database.open) { NSLog(@"create errer"); return; } NSString *sql = @"create table if not exists appointmentslist(" " id integer primary key autoincrement not null, " " appointment_id varchar(128), " " start_time varchar(128), " " end_time varchar(128), " " available varchar(128), " " client_id varchar(128), " " status varchar(128), " " reason varchar(128), " " owner varchar(128), " " client_owner varchar(128) " ");"; BOOL b = [_database executeUpdate:sql]; if (!b) { NSLog(@"create list errer"); } }
-(void)updateRecordWithDictionary:(NSDictionary *)dictionary recordType:(RecordType)recordType { NSString *sql = @"update lawyerlist set address_1=?,address_2=?,city=?,state=?,zip=?,email=?,phone=?,website=? where lawyer_id=? and recordType=?"; BOOL isOk = [_database executeUpdate:sql, dictionary[@"address_1"], dictionary[@"address_2"], dictionary[@"city"], dictionary[@"state"], dictionary[@"zip"], dictionary[@"email"], dictionary[@"phone"], dictionary[@"website"], dictionary[@"id"],[NSString stringWithFormat:@"%d",recordType] ]; if (!isOk) { NSLog(@"update errer"); } }