#import <Foundation/Foundation.h>
@interface APPImageManager : NSObject
+ (instancetype)shareInstance:(NSString *)DBName;
- (void)openDB:(NSString *)DBName;
- (void)createTableName:(NSString *)tableName;
- (void)insertTableName:(NSString *)tableName
ImagesName:(NSString *)imagesName
imageFile:(NSString *)imageFile
location:(NSString *)location
data:(NSString *)data
description:(NSString *)description
lon:(NSString *)lon
lat:(NSString *)lat
photoFileId:(NSString *)photoFileId
photoURL:(NSString *)photoURL;
- (NSMutableArray *)selectAllTableName:(NSString *)imagesName;
- (void)closeDB;
#import "APPImageManager.h"
#import "FMDB.h"
@implementation APPImageManager
static APPImageManager *sqliteManager;
static FMDatabase *db = nil;
+(instancetype)shareInstance:(NSString *)DBName
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sqliteManager = [[APPImageManager alloc] init];
[sqliteManager openDB:DBName];
});
return sqliteManager;
}
- (NSString *)pathForDataBaseWithPathName:(NSString *)pathComponent
{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:pathComponent];
}
- (void)openDB:(NSString *)DBName
{
if (db != nil) {
return;
}
else
{
db = [FMDatabase databaseWithPath:[self pathForDataBaseWithPathName:[NSString stringWithFormat: @"%@.sqlite",DBName]]];
if (![db open]) {
NSLog(@"打开数据库错误");
return;
}
}
}
//number INTEGER
- (void)createTableName:(NSString *)tableName
{
[db open];
NSString *sqlStr = [NSString stringWithFormat:@"CREATE TABLE '%@' (imageName TEXT, ImagePath TEXT , Location TEXT, Data TEXT, Description TEXT, LON TEXT, LAT TEXT, PhotoFileId TEXT, PhotoURL TEXT)",tableName];
[db executeUpdate:sqlStr];
}
- (void)insertTableName:(NSString *)tableName ImagesName:(NSString *)imagesName imageFile:(NSString *)imageFile location:(NSString *)location data:(NSString *)data description:(NSString *)description lon:(NSString *)lon lat:(NSString *)lat photoFileId:(NSString *)photoFileId photoURL:(NSString *)photoURL
{
[db open];
NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO '%@' (imageName , ImagePath , Location, Data, Description, LON, LAT,PhotoFileId,PhotoURL) VALUES(?,?,?,?,?,?,?,?,?)",tableName];
[db executeUpdate:sqlStr,imagesName,imageFile,location,data,description,lon,lat,photoFileId,photoURL];
}
//添加图片描述
-(void)updateTableName:(NSString *)tableName withDescription:(NSString *)description
{
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:@"UPDATE '%@' SET Description = '%@'",tableName,description];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
}
}
-(NSMutableArray *)selectAllTableName:(NSString *)imagesName
{
[db open];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:1];
NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM '%@'",imagesName];
FMResultSet *fmResultSet = [db executeQuery:sqlStr];
while ([fmResultSet next]) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:1];
[dic setObject:[fmResultSet stringForColumn:@"imageName"] forKey:@"imageName"];
[dic setObject:[fmResultSet stringForColumn:@"ImagePath"] forKey:@"ImagePath"];
[dic setObject:[fmResultSet stringForColumn:@"Location"] forKey:@"Location"];
[dic setObject:[fmResultSet stringForColumn:@"Data"] forKey:@"Data"];
[dic setObject:[fmResultSet stringForColumn:@"Description"]forKey:@"Description"];
[dic setObject:[fmResultSet stringForColumn:@"LON"] forKey:@"LON"];
[dic setObject:[fmResultSet stringForColumn:@"LAT"] forKey:@"LAT"];
[dic setObject:[fmResultSet stringForColumn:@"PhotoFileId"]forKey:@"PhotoFileId"];
[dic setObject:[fmResultSet stringForColumn:@"PhotoURL"] forKey:@"PhotoURL"];
[array addObject:dic];
}
return array;
}
-(void)closeDB
{
[db close];
}
FMDB的调用
//数据库
APPImageManager *imageManager = [APPImageManager shareInstance:APPImageDB];
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyyMMddHHmmss"];
locationString=[dateformatter stringFromDate:senddate];
NSDateFormatter *dateStrFormatter=[[NSDateFormatter alloc] init];
[dateStrFormatter setDateFormat:@"yyyy/MM/dd"];
dataStr = [dateStrFormatter stringFromDate:senddate];
NSString *path = [ArchiverFile documentFolder:APPImageDB target:self];
NSString *ImagePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png",locationString]];
if ([self saveIamge:photo path:ImagePath]) {
[HUDManager hideHUDView];
[imageManager createTableName:APPImageDB];
[imageManager insertTableName:APPImageDB ImagesName:locationString imageFile:ImagePath location:[AccountManager sharedInstance].account.adress data:dataStr description:@"" lon:[AccountManager sharedInstance].account.longitude lat:[AccountManager sharedInstance].account.latitude photoFileId:@"" photoURL:@""];
[imageManager closeDB] ;
[HUDManager showMBTips:@"保存成功" inView:self.view];
if ([self.type isEqualToString:@"camera"]) {
[_camera startRunning];
[TGCameraSlideView hideSlideUpView:_slideUpView slideDownView:_slideDownView atView:_captureView completion:^{
_gridButton.enabled =
_toggleButton.enabled =
_shotButton.enabled =
_flashButton.enabled = YES;
}];
if ( [_delegate respondsToSelector:@selector(cameraWillTakePhoto)]) {
[_delegate cameraWillTakePhoto];
}
}else{
if ([_delegate respondsToSelector:@selector(cameraDidTakePhoto:imagePath:)]) {
_photo = newImage;
[_delegate cameraDidTakePhoto:_photo imagePath:ImagePath];
}
}
}else{
[HUDManager hideHUDView];
[HUDManager showMBTips:@"保存失败" inView:self.view];
}
FMDB的使用
最新推荐文章于 2018-12-16 18:02:44 发布