UI day 20 iOS FMDB自己封装的单例类

来源:互联网 发布:feynman 知乎 编辑:程序博客网 时间:2024/05/18 01:42
#import
#import
"FMDatabase.h"
@interfaceDataBaseHelper : NSObject
+ (
DataBaseHelper*)sharedDataBaseHelper;
@property(nonatomic,strong)FMDatabase *db;
@end


//
//  DataBaseHelper.m
//  FMDB
//
//  Created by lanouhn on 15/8/25.
//  Copyright (c) 2015Congwang. 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= [FMDatabasedatabaseWithPath:filePath];
}
//创建表
- (
void)createTable{
   
if ([self.dbopen]) {
       
BOOL result = [self.dbexecuteUpdate:@"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.dbclose];
    }
else{
       
NSLog(@"数据库打开失败");
    }
}
//插入操作
- (
void)insertContact:(Contact*)contact{
   
if ([self.dbopen]) {
       
BOOL result = [self.dbexecuteUpdate:@"INSERT INTO t_contact (name, phoneNum) VALUES (?,?);", contact.name, contact.phoneNum];
       
if (result) {
           
NSLog(@"创建成功");
        }
else{
           
NSLog(@"创建失败");
        }
        [
self.dbclose];
    }
else{
       
NSLog(@"数据库打开失败");
    }
}
//删除
- (
void)deleteContact:(Contact*)contact{
   
if ([self.dbopen]) {
       
BOOL result = [self.dbexecuteUpdate:@"delete from t_contact where phoneNum = ?",contact.phoneNum];
       
if (result) {
           
NSLog(@"创建成功");
        }
else{
           
NSLog(@"创建失败");
        }
        [
self.dbclose];
    }
else{
       
NSLog(@"数据库打开失败");
    }
}
//修改数据
- (
void)updateContact:(Contact*)contact
{
   
if ([self.dbopen]) {
       
BOOL result = [self.dbexecuteUpdate:@"UPDATE t_contact SET name = ?,phoneNum = ? WHERE phoneNum = ?",
                       contact.
name,contact.phoneNum,contact.phoneNum];
       
if (result) {
           
NSLog(@"修改成功");
        }
       
else
        {
           
NSLog(@"修改失败");
        } [
self.dbclose];
    }
else{
       
NSLog(@"数据库打开失败");
    }
}
//查询
- (
NSArray *)queryContact{
   
NSMutableArray *arr = [NSMutableArrayarray];
   
if ([self.dbopen]) {
       
FMResultSet *set = [self.dbexecuteQuery:@"SELECT * FROM t_contact"];
       
while ([set next]) {
           
NSIntegerID = [set intForColumn:@"id"];
           
NSString*name = [set stringForColumn:@"name"];
           
NSString*phoneNum = [set stringForColumn:@"phoneNum"];
           
Contact *contact = [[Contactalloc]initWithName:namephoneNum:phoneNum];
            [arr
addObject:contact];
        }
    }
   
return arr;
}



@end
0 0
原创粉丝点击