当前位置:首页 > 操作系统 > Ios

iOS中FMDB的使用【单例】

DYDB.h
Objective-C
<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>12345678910111213141516 <TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘> <PRE class="line-pre" style=‘padding: 0px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import <Foundation/Foundation.h>
#import <FMDB/FMDatabase.h>
@interfaceDYDB : NSObject{
}
@property(nonatomic,readonly)FMDatabase*database;
+(DYDB*)sharedDB;
-(FMDatabase*)connect;
-(void)clearDB;
@end
DYDB.m
Objective-C
<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 <TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 937px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘> <PRE class="line-pre" style=‘padding: 0px; width: 937px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import “DYDB.h”
#define kDYDBObvName @”dyobv.sqlite”
@implementationDYDB
staticDYDB*_sharedDB;
+(DYDB*)sharedDB{
if(!_sharedDB){
_sharedDB=[[DYDBalloc]init];
}
return_sharedDB;
}
-(id)init{
self=[superinit];
if(self){
NSString*docsdir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
NSString*dbpath=[docsdirstringByAppendingPathComponent:kDYDBObvName];
DLog(@”database file path: %@”,dbpath);
_database=[FMDatabasedatabaseWithPath:dbpath];
}
returnself;
}
-(FMDatabase*)connect{
if([_databaseopen]){
return_database;
}
DLog(@”fail to open db…”);
returnnil;
}
-(void)clearDB{
dispatch_async(dispatch_get_main_queue(),^{
if([_databaseclose]){
_database=nil;
}
});
}
@end
DYLogKeeper+DB.h
Objective-C
<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>1234567891011121314151617181920 <TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘> <PRE class="line-pre" style=‘padding: 0px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import “DYLogKeeper.h”
#import <FMDB/FMDatabase.h>
DYLogKeeper*rs2logkeeper(FMResultSet*rs);
@interfaceDYLogKeeper(DB)
+(void)createSqliteTable;
+(BOOL)insert:(DYLogKeeper*)logkeeper;
+(BOOL)updateContent:(NSString*)contentlocalId:(NSString*)localId;
+(BOOL)remove:(DYLogKeeper*)logkeeperId;
+(DYLogKeeper*)findById:(NSString*)localId;
+(NSArray*)findOfStartDate:(NSDate*)starttoDate:(NSDate*)toDate;
@end
DYLogKeeper+DB.m
Objective-C
<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 <TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 1189px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘> <PRE class="line-pre" style=‘padding: 0px; width: 1189px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import “DYLogKeeper+DB.h”
#import “DYDB.h”
#import <FMDB/FMDatabase.h>
#import “DYUUID.h”
#import “NSDate+NSDateFormaterCategory.h”
DYLogKeeper*rs2logkeeper(FMResultSet*rs){
DYLogKeeper*obj=[[DYLogKeeperalloc]init];
obj.localId=[rsstringForColumn:@”local_id”];
obj.logkeeperId=[rsstringForColumn:@”logkeeper_id”];
obj.addtime=[rsdateForColumn:@”add_time”];
obj.content=[rsstringForColumn:@”content”];
obj.deviceId=[rsstringForColumn:@”device_id”];
obj.deviceType=[rsintForColumn:@”device_type”];
obj.channel=[rsintForColumn:@”channel”];
returnobj;
}
@implementationDYLogKeeper(DB)
+(void)createSqliteTable{
DLog(@”check table is exists?”);
FMDatabase*db=[[DYDBsharedDB]connect];
NSString*existsSql=[NSStringstringWithFormat:@”select count(name) as countNum from sqlite_master where type = ‘table’ and name = ‘%@’”,@”log_keepers”];
DLog(@”%@”,existsSql);
FMResultSet*rs=[dbexecuteQuery:existsSql];
if([rsnext]){
NSIntegercount=[rsintForColumn:@”countNum”];
DLog(@”The table count: %d”,count);
if(count==1){
DLog(@”log_keepers table is existed.”);
return;
}
DLog(@”log_keepers is not existed.”);
}
[rsclose];
DLog(@”create table ….”);
NSString*filePath=[[NSBundlemainBundle]pathForResource:@”log_keepers_table”ofType:@”sql”];
DLog(@”logkeeper sql file: %@”,filePath);
NSError*error;
NSString*sql=[NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:&error];
if(error!=nil){
DLog(@”fail to read sql file: %@”,[errordescription]);
return;
}
DLog(@”—sql:n%@”,sql);
DLog(@”execute sql ….”);
if([dbexecuteUpdate:sql]){
DLog(@”create table succes….”);
}else{
DLog(@”fail to execute update sql..”);
}
[dbclose];
}
+(BOOL)insert:(DYLogKeeper*)logkeeper{
DLog(@”insert logkeeper”);
DLog(@”convert int value to NSNumber …”);
logkeeper.localId=[DYUUIDuuidString];
NSNumber*channelNum=[NSNumbernumberWithInt:logkeeper.channel];
DLog(@”— channelNum: %@”,channelNum);
NSNumber*typeNumber=[NSNumbernumberWithInt:logkeeper.deviceType];
DLog(@”— deviceType: %@”,typeNumber);
NSString*sql=@”insert into log_keepers(local_id, logkeeper_id, add_time, content, device_id, device_type, channel) values(?, ?, ?, ?, ?, ?, ?)”;
FMDatabase*db=[[DYDBsharedDB]connect];
if(db==nil){
DLog(@”fail to create db..”);
}
BOOLret=[dbexecuteUpdate:sql,logkeeper.localId,logkeeper.logkeeperId,
logkeeper.addtime,logkeeper.content,
logkeeper.deviceId,typeNumber,channelNum];
[dbclose];
returnret;
}
+(BOOL)updateContent:(NSString*)content
localId:(NSString*)localId{
DLog(@”update logkeeper content”);
NSString*sql=@”update log_keepers set content = ? where local_id = ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
BOOLret=[dbexecuteUpdate:sql,content,localId];
[dbclose];
returnret;
}
-(BOOL)remove:(DYLogKeeper*)logkeeper{
DLog(@”remove logkeeper: %@”,logkeeper.localId);
NSString*sql=@”delete from log_keepers where local_id = ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
BOOLret=[dbexecuteUpdate:sql,logkeeper.localId];
[dbclose];
returnret;
}
-(DYLogKeeper*)findById:(NSString*)localId{
DLog(@”find logkeeper by id: %@”,localId);
FMDatabase*db=[[DYDBsharedDB]connect];
FMResultSet*rs=[dbexecuteQuery:@”select * from log_keepers where local_id = ?”,localId];
DYLogKeeper*ret;
if([rsnext]){
ret=rs2logkeeper(rs);
}
[dbclose];
returnret;
}
+(NSArray*)findOfStartDate:(NSDate*)starttoDate:(NSDate*)toDate{
DLog(@”find logkeeper between date ….”);
NSString*sql=@”select * from log_keepers where add_time between ? and ?”;
FMDatabase*db=[[DYDBsharedDB]connect];
FMResultSet*rs=[dbexecuteQuery:sql,start,toDate];
NSMutableArray*array=[NSMutableArrayarrayWithCapacity:32];
while([rsnext]){
DYLogKeeper*logkeeper=rs2logkeeper(rs);
[arrayaddObject:logkeeper];
}
[rsclose];
[dbclose];
returnarray;
}
@end
DYLogKeeper.h
Objective-C
<TD class="line-numbers" style=‘padding: 12px 6px; text-align: right; color: rgb(170, 170, 170); line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px; border-right-color: rgb(238, 238, 238); border-right-width: 1px; border-right-style: solid; min-width: 27px; box-sizing: border-box; -webkit-user-select: none;‘>1234567891011121314151617181920 <TD class="line-data" style=‘padding: 12px 0px 12px 12px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 12px;‘> <PRE class="line-pre" style=‘padding: 0px; width: 744px; line-height: 1.4; font-family: Consolas, "Liberation Mono", Courier, monospace; margin-top: 0px; margin-bottom: 0px;‘>
#import <Foundation/Foundation.h>
@interfaceDYLogKeeper : NSObject
@property(strong,nonatomic)NSString*localId;
@property(strong,nonatomic)NSDate*addtime;
@property(strong,nonatomic)NSString*deviceId;
@property(strong,nonatomic)NSString*content;
@property(strong,nonatomic)NSString*logkeeperId;
@property(nonatomic)intdeviceType;
@property(nonatomic)intchannel;
@end




原文:http://www.cnblogs.com/suenihy/p/3525732.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!