iOS FMDB自己封装的单例类

来源:互联网 发布:郑州直销软件靠谱吗 编辑:程序博客网 时间:2024/06/05 23:54
//
//  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 lanouhn 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 = [setstringForColumn:@"phoneNum"];
           
 Contact *contact = [[Contact alloc]initWithName:name phoneNum:phoneNum];
            [arr
 addObject:contact];
        }
    }
   
 return arr;
}



@end

0 0