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
// 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
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 数据库链接字符串查询网站
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- DB2实例管理
- DB2实例管理
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解