码迷,mamicode.com
首页 > 数据库 > 详细

FMDB的增删改查

时间:2014-07-29 13:51:18      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:os   for   ar   amp   sql   type   res   app   

+ (ZSDataHandle *)shareDataHandle

{

    static ZSDataHandle *dataHandle = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        if (dataHandle == nil) {

            dataHandle = [[ZSDataHandle alloc] init];

        }

    });

    

    return dataHandle;

}

 

- (instancetype)init

{

    self = [super init];

    if (self) {

        NSString *filePaht = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

        NSLog(@"%@",filePaht);

        NSString *dbPath = [filePaht stringByAppendingString:@"zs.sqlite"];

        _db = [FMDatabase databaseWithPath:dbPath];

        if ([_db open]) {

        

            BOOL result = [_db executeUpdate:@"create table Students (number Integer Primary Key autoIncrement,age NSInteger default 15,name text not NULL)"];

            NSLog(@"%d",result);

            [_db close];

        }

    }

    

    return self;

}

 

//增加

- (void)insertWithNumber:(NSInteger)number name:(NSString *)name age:(NSInteger)age

{

    if ([_db open]) {

        BOOL result = [_db executeUpdate:@"insert into Students (number,name,age) values (?,?,?)",[NSNumber numberWithInteger:number],name,[NSNumber numberWithInteger:age]];

        NSLog(@"%d",result);

        [_db close];

    }

}

 

//删除

- (void)deleteWithnumber:(NSInteger)number

{

    if ([_db open]) {

        BOOL result = [_db executeUpdate:@"delete from Students where number = ?",[NSNumber numberWithInteger:number]];

        NSLog(@"%d",result);

        [_db close];

    }

}

 

//查询

- (NSMutableArray *)selectWithWithNumber:(NSInteger)number

{

    NSMutableArray *array = [NSMutableArray array];

    if ([_db open]) {

        FMResultSet *result = [_db executeQuery:@"select name from Students where number = ?",[NSNumber numberWithInteger:number]];

        if ([result next]) {

            [array addObject:[result stringForColumn:@"name"]];

            

        }

        

        [_db close];

    }

    

    return array;

}

 

FMDB的增删改查,布布扣,bubuko.com

FMDB的增删改查

标签:os   for   ar   amp   sql   type   res   app   

原文地址:http://www.cnblogs.com/zhangshan/p/3875159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!