UI day 20 iOS FMDB自己封装的单例类
来源:互联网 发布:feynman 知乎 编辑:程序博客网 时间:2024/05/18 01:42
#import
#import "FMDatabase.h"
@interfaceDataBaseHelper : NSObject
+ (DataBaseHelper*)sharedDataBaseHelper;
@property(nonatomic,strong)FMDatabase *db;
#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) 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= [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
// 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= [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
- UI day 20 iOS FMDB自己封装的单例类
- iOS FMDB自己封装的单例类
- iOS FMDB自己封装的单例类
- iOS FMDB自己封装的单例类
- iOS简单runtime封装fmdb的使用
- fmdb的封装
- FMDB的简单封装
- FMDB的简单封装
- FMDB的二次封装
- ios 对FMDB 简单封装
- 【ios开发】使用FMDB封装类的sqlite数据库操作
- Base封装(二)-- 封装属于自己的Library(UI篇)
- FMDB-一个sqlite的封装
- FMDB-一个sqlite的封装
- 对fmdb简单的封装
- 对FMDB的封装JRDB
- IOS-20-数据库操作之(SQLite第三方封装库FMDB)的基础入门使用及demo源码
- iOS—SQLite 3封装库FMDB
- 线性插补法查找
- HDU1176 免费馅饼 经典DP
- UI day 19 UICollectionView
- Git@OSC代码托管(含Git教程!important非命令行)
- 选择排序复习
- UI day 20 iOS FMDB自己封装的单例类
- UI day 21 程序 进程 线程 GCD 共产党
- java并发编程学习之脏读代码示例及处理
- LeetCode 14:Longest Common Prefix
- UI day 22 block动画 CAlayer动画
- 工程优化作业——成功失败法和黄金分割法
- Android Service使用Messenger通信
- 129Sum Root to Leaf Numbers
- PHP学习练手(三)