您的位置:首页 > 移动开发 > IOS开发

iOS FMDB自己封装的单例类

2016-06-20 16:27 281 查看
//

//  DataBaseHelper.h

//  FMDB

//

//  Created by 王聪 on 14/8/25.

//  Copyright (c) 2014年 Congwang. All rights reserved.

//

#import

#import "FMDatabase.h"

@interface DataBaseHelper : NSObject

+ (DataBaseHelper *)sharedDataBaseHelper;

@property (nonatomic, strong) FMDatabase *db;

@end

//

//  DataBaseHelper.m

//  FMDB

//

//  Created by wangcong on 15/8/25.

//  Copyright (c) 2015年 Congwang. All rights reserved.

//

#import "DataBaseHelper.h"

#import "Contact.h"

static DataBaseHelper*helper = nil;

@implementation DataBaseHelper

+ (DataBaseHelper *)sharedDataBaseHelper{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        helper = [[DataBaseHelper alloc] init];

        [helper createDataBase];

        [helper createTable];

    });return helper;

}

//创建数据库

- (void)createDataBase{

    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];

    NSString *filePath = [doc stringByAppendingPathComponent:@"contacts.sqlite"];

    //创建数据库

    self.db = [FMDatabase databaseWithPath:filePath];

}

//创建表

- (void)createTable{

    if ([self.db open]) {

        BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_contact (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, phoneNum text NOT NULL);"];

        if (result) {

            NSLog(@"创建成功");

        }else{

            NSLog(@"创建失败");

        }[self.db close];

    }else{

        NSLog(@"数据库打开失败");

    }

}

//插入操作

- (void)insertContact:(Contact *)contact{

    if ([self.db open]) {

        BOOL result = [self.db executeUpdate:@"INSERT INTO t_contact (name, phoneNum) VALUES (?,?);", contact.name, contact.phoneNum];

        if (result) {

            NSLog(@"创建成功");

        }else{

            NSLog(@"创建失败");

        }

        [self.db close];

    }else{

        NSLog(@"数据库打开失败");

    }

}

//删除

- (void)deleteContact:(Contact *)contact{

    if ([self.db open]) {

        BOOL result = [self.db executeUpdate:@"delete from t_contact where phoneNum = ?",contact.phoneNum];

        if (result) {

            NSLog(@"创建成功");

        }else{

            NSLog(@"创建失败");

        }

        [self.db close];

    }else{

        NSLog(@"数据库打开失败");

    }

}

//修改数据

- (void)updateContact:(Contact *)contact

{

    if ([self.db open]) {

        BOOL result = [self.db executeUpdate:@"UPDATE t_contact SET name = ?,phoneNum = ? WHERE phoneNum = ?",

                       contact.name,contact.phoneNum,contact.phoneNum];

        if (result) {

            NSLog(@"修改成功");

        }

        else

        {

            NSLog(@"修改失败");

        } [self.db close];

    }else{

        NSLog(@"数据库打开失败");

    }

}

//查询

- (NSArray *)queryContact{

    NSMutableArray *arr = [NSMutableArray array];

    if ([self.db open]) {

        FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_contact"];

        while ([set next]) {

            NSInteger ID = [set intForColumn:@"id"];

            NSString *name = [set stringForColumn:@"name"];

            NSString *phoneNum = [set stringForColumn:@"phoneNum"];

            Contact *contact = [[Contact alloc] initWithName:name phoneNum:phoneNum];

            [arr addObject:contact];

        }

    }

    return arr;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息