FMDB的使用

#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];

                }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值