iOS中FMDB的使用【单例】
12345678910111213141516 |
#import <Foundation/Foundation.h>
#import <FMDB/FMDatabase.h>
@interface DYDB : NSObject {
}
@property(nonatomic, readonly) FMDatabase *database;
+ (DYDB *) sharedDB;
- (FMDatabase *) connect;
- (void) clearDB;
@end
|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
#import “DYDB.h”
#define kDYDBObvName @”dyobv.sqlite”
@implementation DYDB
static DYDB *_sharedDB;
+ (DYDB *) sharedDB {
if (!_sharedDB) {
_sharedDB = [[DYDB alloc] init];
}
return _sharedDB;
}
- (id) init {
self = [super init];
if (self) {
NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dbpath = [docsdir stringByAppendingPathComponent:kDYDBObvName];
DLog(@”database file path: %@”, dbpath);
_database = [FMDatabase databaseWithPath:dbpath];
}
return self;
}
- (FMDatabase *) connect {
if ([_database open]) {
return _database;
}
DLog(@”fail to open db…”);
return nil;
}
- (void) clearDB {
dispatch_async(dispatch_get_main_queue(), ^{
if([_database close]) {
_database = nil;
}
});
}
@end
|
1234567891011121314151617181920 |
#import “DYLogKeeper.h”
#import <FMDB/FMDatabase.h>
DYLogKeeper * rs2logkeeper(FMResultSet *rs);
@interface DYLogKeeper (DB)
+ (void) createSqliteTable;
+ (BOOL) insert: (DYLogKeeper *) logkeeper;
+ (BOOL) updateContent: (NSString *) content localId: (NSString *) localId;
+ (BOOL) remove: (DYLogKeeper *) logkeeperId;
+ (DYLogKeeper *) findById: (NSString *) localId;
+ (NSArray *) findOfStartDate: (NSDate *) start toDate:(NSDate *) toDate;
@end
|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
#import “DYLogKeeper+DB.h”
#import “DYDB.h”
#import <FMDB/FMDatabase.h>
#import “DYUUID.h”
#import “NSDate+NSDateFormaterCategory.h”
DYLogKeeper * rs2logkeeper(FMResultSet *rs) {
DYLogKeeper *obj = [[DYLogKeeper alloc] init];
obj.localId = [rs stringForColumn:@”local_id”];
obj.logkeeperId = [rs stringForColumn:@”logkeeper_id”];
obj.addtime = [rs dateForColumn:@”add_time”];
obj.content = [rs stringForColumn:@”content”];
obj.deviceId = [rs stringForColumn:@”device_id”];
obj.deviceType = [rs intForColumn:@”device_type”];
obj.channel = [rs intForColumn:@”channel”];
return obj;
}
@implementation DYLogKeeper (DB)
+ (void) createSqliteTable {
DLog(@”check table is exists?”);
FMDatabase *db = [[DYDB sharedDB] connect];
NSString *existsSql = [NSString stringWithFormat:@”select count(name) as countNum from sqlite_master where type = ‘table’ and name = ‘%@’”, @”log_keepers” ];
DLog(@”%@”, existsSql);
FMResultSet *rs = [db executeQuery:existsSql];
if ([rs next]) {
NSInteger count = [rs intForColumn:@”countNum”];
DLog(@”The table count: %d”, count);
if (count == 1) {
DLog(@”log_keepers table is existed.”);
return;
}
DLog(@”log_keepers is not existed.”);
}
[rs close];
DLog(@”create table ….”);
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”log_keepers_table” ofType:@”sql”];
DLog(@”logkeeper sql file: %@”, filePath);
NSError *error;
NSString *sql = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
DLog(@”fail to read sql file: %@”, [error description]);
return;
}
DLog(@”—sql: \n %@”, sql);
DLog(@”execute sql ….”);
if([db executeUpdate:sql]) {
DLog(@”create table succes….”);
} else {
DLog(@”fail to execute update sql..”);
}
[db close];
}
+ (BOOL) insert: (DYLogKeeper *) logkeeper {
DLog(@”insert logkeeper”);
DLog(@”convert int value to NSNumber …”);
logkeeper.localId = [DYUUID uuidString];
NSNumber *channelNum = [NSNumber numberWithInt:logkeeper.channel];
DLog(@”— channelNum: %@”, channelNum);
NSNumber *typeNumber = [NSNumber numberWithInt: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 = [[DYDB sharedDB] connect];
if (db == nil) {
DLog(@”fail to create db..”);
}
BOOL ret = [db executeUpdate:sql, logkeeper.localId, logkeeper.logkeeperId,
logkeeper.addtime, logkeeper.content,
logkeeper.deviceId, typeNumber, channelNum];
[db close];
return ret;
}
+ (BOOL) updateContent:(NSString *)content
localId: (NSString *)localId {
DLog(@”update logkeeper content”);
NSString *sql = @”update log_keepers set content = ? where local_id = ?”;
FMDatabase *db = [[DYDB sharedDB] connect];
BOOL ret = [db executeUpdate:sql, content, localId];
[db close];
return ret;
}
- (BOOL) remove: (DYLogKeeper *) logkeeper {
DLog(@”remove logkeeper: %@”, logkeeper.localId);
NSString *sql = @”delete from log_keepers where local_id = ?”;
FMDatabase *db = [[DYDB sharedDB] connect];
BOOL ret = [db executeUpdate:sql, logkeeper.localId];
[db close];
return ret;
}
- (DYLogKeeper *) findById:(NSString *)localId {
DLog(@”find logkeeper by id: %@”, localId);
FMDatabase *db = [[DYDB sharedDB] connect];
FMResultSet *rs = [db executeQuery:@”select * from log_keepers where local_id = ?”, localId];
DYLogKeeper *ret;
if ([rs next]) {
ret = rs2logkeeper(rs);
}
[db close];
return ret;
}
+ (NSArray *) findOfStartDate: (NSDate *) start toDate:(NSDate *) toDate {
DLog(@”find logkeeper between date ….”);
NSString *sql = @”select * from log_keepers where add_time between ? and ?”;
FMDatabase *db = [[DYDB sharedDB] connect];
FMResultSet *rs = [db executeQuery:sql, start, toDate];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:32];
while ([rs next]) {
DYLogKeeper *logkeeper = rs2logkeeper(rs);
[array addObject:logkeeper];
}
[rs close];
[db close];
return array;
}
@end
|
1234567891011121314151617181920 |
#import <Foundation/Foundation.h>
@interface DYLogKeeper : 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) int deviceType;
@property(nonatomic) int channel;
@end
|
iOS中FMDB的使用【单例】的更多相关文章
- iOS中浅淡UIApplication单例-b
在iOS的操作系统中 每一个程序都对应一个application单例,每一个application都对应一个Appdelegate代理,在代理中控制程序的各个状态.我们在程序中获取Applicatio ...
- RPCZ中的智能指针单例
RPCZ中的智能指针单例 (金庆的专栏) 智能指针单例应用于 RPCZ 库以实现库的自动初始化与自动清理. RPCZ: RPC implementation for Protocol Buffers ...
- spring中如何向一个单例bean中注入非单例bean
看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作 @Component public class People{ @Autowired private Man man; } 这里如 ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- c++单例模式为什么不在析构函数中释放静态的单例对象(转)
需要清楚一下几点: 1.单例中的 new 的对象需要delete释放. 2.delete释放对象的时候才会调用对象的析构函数. 3.如果在析构函数里调用delete,那么程序结束时,根本进 ...
- springmvc中的controller是单例的
今天发现spring3中的controller默认是单例的,若是某个controller中有一个私有的变量a,所有请求到同一个controller时,使用的a变量是共用的,即若是某个请求中修改了这个变 ...
- 【iOS】ARC-MRC下的单例及其应用
单例的应用十分普遍,单例模式使一个类仅仅有一个实例. *易于供外界訪问. *方便控制实例个数,节约系统资源. *OC中的常见单例: 如:UIApplication, NSNotificationCe ...
- IOS设计模式第二篇之单例设计模式
现在我们的组件已经有组织了.你需要从其他的地方得到数据,你也可以创建一个API类管理数据这个下个设计模式单例里面介绍. 这个单例设计模式确保这个类仅仅拥有一个实例,并且为这个实例提供一个全局的访问点. ...
- Egret中的三种单例写法
1 普通的单例写法 as3中也是这么个写法. 缺点:每个单例类里都要写instance和getInstance. class Single{ private static instance:Singl ...
- SpringMVC中的Controller默认单例
众所周知,Servlet是单例的. 在struts中,Action是多例的,每一个请求都会new出来一个action来处理. 在Spring中,Controller默认是单例的,多个请求都会访问同一个 ...
随机推荐
- C++ 拆分字符串-copy()
c++拆分字符串方法: #include <iostream>#include <string>#include <sstream>#include <alg ...
- MySQL 使用mysqld_multi部署单机多实例详细过程 (转)
随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然 5.6版本已经在这方面改进很多,但是也没有达到100%,所以为了充分的利用系统 ...
- Linux下Memcached-1.4.10安装
memcache是一款流行的缓存产品,它分为两个部分:一个是运行在服务器端的memcached进程,一个是在客户端进行调用获取缓存中数据客户端,例如比较常用的PHP客户端.这里,记录一下安装服务器端的 ...
- html中间块居中宽度自适应
说来,这个其实不是个多难的事情,但是,若没有经验或者没有了解过html原数在浏览器中显示的顺序,可能还真是个问题,不知如何调整. 先说明下,在确定了左右两边显示的块的宽度后,再让中间块的宽度自适应,这 ...
- js获取网页的各种高度和宽度
document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...
- 怎样进行Android UI元素设计
Android UI元素里面包含了许多的内容,比如:该平台由操作系统.中间件.用户界面和应用软件组成,一个应用程序要想受用户喜爱,那么UI可不能差. Android为相似的编程名词引入了一些新的术语, ...
- TMS320C54x系列DSP的CPU与外设——第1章 绪论
第1章 绪论 TMS320C54x DSP是TMS320系列DSP产品中的定点数字信号处理器.C54x DSP满足了实时嵌入式应用的一些要求,例如通信方面的应用. C54x的中央处理单元(CPU)具有 ...
- 关于List泛型的强制转换
当我们从数据库中查询出一些数据,有时返回的结果可能是List<Object>类型,而我们清楚的知道它的准确类型是List<User>,可能我们想直接的去进行类型的转换,你可能会 ...
- mysql给日期增减
有个需求就是判断过期的供求信息,如果用户刷新了则判断过期日期是否小于现在,如果是则自动推迟7天. IF(expire<NOW(),DATE_ADD(NOW(), INTERVAL 7 DAY), ...
- SteamVR Unity工具包(VRTK)之激光和移动
简单激光指针(VRTK_ SimplePointer) 简单指针(Simple Pointer)脚本从控制器尾部发出一个有色光束来模拟激光束.这在场景中指向对象很有用,它能判断所指向的对象以及对象距控 ...