博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS-FMDB
阅读量:6251 次
发布时间:2019-06-22

本文共 3819 字,大约阅读时间需要 12 分钟。

hot3.png

#import 
#import
#import "MyModel.h"@interface FMDBManager : NSObject{ FMDatabase *_dataBase;}+(instancetype)shareInstance;- (BOOL)insert:(MyModel *)model;- (BOOL)deleteWithId:(NSString *)Id;- (BOOL)update:(NSString *)Id key:(NSString *)key value:(NSString *)value;- (NSMutableArray *)selectLimit:(NSInteger)index number:(NSInteger)number;- (NSMutableArray *)selectAll;- (NSUInteger)count;- (BOOL)isInclude:(NSString *)Id;@end

 

#import "FMDBManager.h"@implementation FMDBManager+ (instancetype)shareInstance{    static FMDBManager *manager;        static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        manager = [[FMDBManager alloc] init];    });        return manager;}- (instancetype)init{    if (self = [super init]) {                NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/dataBase.db"];                _dataBase = [[FMDatabase alloc] initWithPath:path];        [_dataBase open];        NSString *sql = @"create table if not exists personTable(id integer primary key autoincrement,ID varchar(256), name varchar(256),age varchar(256),height varchar(256),sex varchar(256))";                if ([_dataBase executeUpdate:sql]) {            NSLog(@"建表成功");        } else {            NSLog(@"建表失败");        };    }    return self;}- (BOOL)insert:(MyModel *)model{    NSString *sql = @"insert into personTable(ID, name, age, height, sex)values(?,?,?,?,?)";        if ([_dataBase executeUpdate:sql,model.ID, model.name, model.age, model.height, model.sex]) {        return YES;    } else {        return NO;    }}- (BOOL)deleteWithId:(NSString *)Id{    NSString *sql = @"delete from personTable where ID = ?";        if ([_dataBase executeUpdate:sql,Id]) {        return YES;    } else {        return NO;    }}- (BOOL)update:(NSString *)Id key:(NSString *)key value:(NSString *)value{    NSString *sql = [NSString stringWithFormat:@"update personTable set %@ = ? where ID = ?",key];        if ([_dataBase executeUpdate:sql,value,Id]) {        return YES;    } else {        return NO;    }}- (NSMutableArray *)selectLimit:(NSInteger)index number:(NSInteger)number{    NSString *sql = [NSString stringWithFormat:@"select *from personTable limit %ld, %ld",(long)index, (long)number];    FMResultSet *s = [_dataBase executeQuery:sql];    NSMutableArray *array = [[NSMutableArray alloc]init];        while ([s next]) {                MyModel *model = [[MyModel alloc] init];                model.ID = [s stringForColumn:@"ID"];        model.name = [s stringForColumn:@"name"];        model.age = [s stringForColumn:@"age"];        model.height = [s stringForColumn:@"height"];        model.sex = [s stringForColumn:@"sex"];                [array addObject:model];    }        return array;}- (NSMutableArray *)selectAll{    FMResultSet *s = [_dataBase executeQuery:@"select *from personTable"];        NSMutableArray *array = [[NSMutableArray alloc]init];        while ([s next]) {                MyModel *model = [[MyModel alloc] init];                model.ID = [s stringForColumn:@"ID"];        model.name = [s stringForColumn:@"name"];        model.age = [s stringForColumn:@"age"];        model.height = [s stringForColumn:@"height"];        model.sex = [s stringForColumn:@"sex"];                [array addObject:model];    }        return array;}- (NSUInteger)count{    FMResultSet *s = [_dataBase executeQuery:@"select count(*) from personTable"];        NSUInteger c = 0;        while ([s next]) {        c = [s intForColumnIndex:0];    }        return c;}- (BOOL)isInclude:(NSString *)Id{    NSString *sql = @"select *from personTable where ID = ?";        FMResultSet *set = [_dataBase executeQuery:sql,Id];        while ([set next]) {        return YES;    }    return NO;}@end

 

转载于:https://my.oschina.net/gwlCode/blog/1591526

你可能感兴趣的文章
git锁和钩子以及图形化界面
查看>>
DataSnap Server 客户端调用 异常
查看>>
cesium之地图贴地量算工具效果篇
查看>>
C# winform DevExpress上传图片到数据库【转】
查看>>
指针和引用
查看>>
Review Board
查看>>
winform 程序中 调用wpf 窗体
查看>>
Chapter 24. Dynamic language support
查看>>
信息检索Reading List
查看>>
Advanced Customization of the jQuery Mobile Buttons | Appcropolis
查看>>
ubuntu配置bridge网桥
查看>>
批量修改sharepoint 2013站点里区域设置
查看>>
在尝试重新安装一个服务时遇到这样的错误:指定服务已标记为删除
查看>>
我的Android开发相关文章
查看>>
20141029
查看>>
Windows Server 2012如果打开网页慢下载快的话
查看>>
【反传销】春节一个短暂误入传销和脱身的真实故事以及对技术的思考(二)回家之路...
查看>>
166. Fraction to Recurring Decimal
查看>>
(转)Java线程:新特征-条件变量
查看>>
建立ORACLE10G DATA GUARD--->Physical Standby
查看>>