#import "FMDBManager.h"
@interface FMDBManager ()
@property (nonatomic, strong) FMDatabase *dataBase;
@end
@implementation FMDBManager
+ (FMDBManager *)shareInstance {
static FMDBManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[FMDBManager alloc] init];
});
return manager;
}
//创建表
- (void)createTableWithTableName:(NSString *)tableName {
[self initFMDB];
if ([self.dataBase open]) {
NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (resourceId INTEGER PRIMARY KEY AUTOINCREMENT, resourceName TEXT, resourceType TEXT, resourceLocalName TEXT, resourceSize TEXT, downProgress TEXT, downLoadStatus Text)",tableName];
BOOL result = [self.dataBase executeUpdate:createSQL];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败");
}
};
[self.dataBase close];
}
//插入
- (void)insertResourceWithResourceType:(NSString *)resourceType
resourceId:(NSInteger )resourceId
resourceName:(NSString *)resourceName
resourceSize:(NSString *)resourceSize
resourceLocalName:(NSString *)resourceLocalName
downLoadProgress:(CGFloat )downLoadProgress
downLoadStatus:(NSString *)downLoadStatus
tableName:(NSString *)tableName{
[self initFMDB];
if ([self.dataBase open]) {
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO '%@' (resourceId, resourceName, resourceType, resourceSize, resourceLocalName, downProgress, downLoadStatus) VALUES ('%ld', '%@', '%@', '%@', '%@', '%f', '%@')", tableName, (long)resourceId, resourceName, resourceType, resourceSize, resourceLocalName, downLoadProgress, downLoadStatus];
BOOL result = [self.dataBase executeUpdate:insertSQL];
if (result) {
NSLog(@"插入成功");
}
}
[self.dataBase close];
}
//查询所有
- (NSMutableArray *)selectAllResourceFromTableName:(NSString *)tableName {
//返回的数组
NSMutableArray *resultArr = [[NSMutableArray alloc] init];
[self initFMDB];
if ([self.dataBase open]) {
NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM %@", tableName];
FMResultSet *result = [self.dataBase executeQuery:selectSQL];
while ([result next]) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSInteger resourceId = [result longForColumn:@"resourceId"];
NSString *resourceName = [result stringForColumn:@"resourceName"];
NSString *resourceType = [result stringForColumn:@"resourceType"];
NSString *resourceSize = [result stringForColumn:@"resourceSize"];
NSString *resourceLocalName = [result stringForColumn:@"resourceLocalName"];
NSString *downLoadProgress = [result stringForColumn:@"downProgress"];
NSString *downLoadStatus = [result stringForColumn:@"downLoadStatus"];
//NSLog(@"___result:___%ld \n %@ \n %@ \n %@",resourceId, resourceName, resourceType, resourceAddr);
//整理成 json 返回
[dic setValue:@(resourceId) forKey:@"resId"];
[dic setValue:resourceName forKey:@"name"];
[dic setValue:resourceType forKey:@"type"];
[dic setValue:resourceSize forKey:@"size"];
[dic setValue:resourceLocalName forKey:@"localName"];
[dic setValue:downLoadProgress forKey:@"progress"];
[dic setValue:downLoadStatus forKey:@"downLoadStatus"];
[resultArr addObject:dic];
}
[result close];
}
[self.dataBase close];
return resultArr;
}
//更新
- (void)upDateLocalNameTable:(NSString *)tableName
localName:(NSString *)localName
resourceName:(NSString *)resourceName {
[self initFMDB];
if ([self.dataBase open]) {
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE %@ SET resourceLocalName = '%@' WHERE resourceName = '%@'", tableName, localName, resourceName];
BOOL result = [self.dataBase executeUpdate:updateSQL];
if (result) {
NSLog(@"~~~~更新成功");
} else {
NSLog(@"~~~~~更新失败");
}
}
[self.dataBase close];
}
//删除
- (void)deleteSelectedItemWithResourceName:(NSString *)resourceName
fromTable:(NSString *)tableName{
[self initFMDB];
if ([self.dataBase open]) {
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where resourceName = '%@'",
tableName, resourceName];
BOOL res = [self.dataBase executeUpdate:deleteSql];
if (res) {
NSLog(@"删除成功");
} else {
NSLog(@"删除失败");
}
}
[self.dataBase close];
}
//删除表
- (void)deleteAllWithTableName:(NSString *)tableName {
[self initFMDB];
if ([self.dataBase open]) {
NSString *clearSQL = [NSString stringWithFormat:@"DROP TABLE IF EXISTS %@", tableName];
BOOL result = [self.dataBase executeUpdate:clearSQL];
if (result) {
NSLog(@"清除表数据成功");
} else {
NSLog(@"清除表数据失败");
}
}
}
//初始化
- (void)initFMDB {
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [doc stringByAppendingPathComponent:@"resource.sqlite"];
NSLog(@"%@", fileName);
self.dataBase = [FMDatabase databaseWithPath:fileName];
}