@interface...">
美文网首页
FMDB业务处理

FMDB业务处理

作者: 野地里的程序员 | 来源:发表于2017-06-30 09:41 被阅读0次

    .h当中的内容

    #impor

    #import "bok.h"

    #import<FMDB.h>

    @interface modell : NSObject

    //实例方法+(instancetype)sharedFM;

    //添加方法-(void)insertstudent:(bok*)student;

    //修改方法-(void)updatastudent:(bok*)student;

    //删除方法-(void)delestudent:(bok*)student;

    //过去数据库中所有数据-(NSMutableArray *)getall;

    @end

    .m当中的内容

    #import "modell.h"

    #import<FMDB.h>

    static modell *mm = nil;

    static FMDatabase *fmdb;

    @implementation modell

    +(instancetype)sharedFM

    {

    if (mm == nil) {

    mm =[[modell alloc] init];

    [mm initdb];

    }

    return mm;

    }

    -(id)copy

    {

    return self;

    }

    -(id)mutableCopy

    {

    return self;

    }

    -(void)initdb

    {

    NSString *docm = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

    NSString *path = [docm stringByAppendingPathComponent:@"lc.db"];

    fmdb = [[FMDatabase alloc] initWithPath:path];

    if ([fmdb open]) {

    [fmdb executeUpdate:@"create table classdata(ids integer primary key autoincrement, name text, age text)"];

    [fmdb close];

    }

    else

    {

    NSLog(@"列表创建失败");

    }

    }

    -(void)insertstudent:(bok*)student

    {

    BOOL isc = false;

    NSArray *arr = [self getall];

    for (bok *s in arr)

    {

    if ([s.name isEqualToString:student.name] && [s.age isEqualToString:student.age])

    {

    isc = true;

    }

    }

    if (isc) {

    NSLog(@"数据相同");

    }

    else

    {

    [fmdb open];

    BOOL isb = [fmdb executeUpdate:@"insert into classdata values(null,?,?)",student.name,student.age];

    if (isb) {

    NSLog(@"添加成功");

    }

    else

    {

    NSLog(@"添加失败");

    }

    [fmdb close];

    }

    }

    -(void)updatastudent:(bok*)student

    {

    [fmdb open];

    NSString

    *str = [NSString stringWithFormat:@"update classdata set name = '%@',

    age = '%@' where ids = %ld",student.name,student.age,student.IDs];

    BOOL isd = [fmdb executeUpdate:str];

    if (isd) {

    NSLog(@"修改成功");

    }

    else

    {

    NSLog(@"修改失败");

    }

    [fmdb close];

    }

    -(void)delestudent:(bok*)student

    {

    [fmdb open];

    NSString *sql = [NSString stringWithFormat:@"delete from classdata where ids = %ld",student.IDs];

    BOOL isd = [fmdb executeUpdate:sql];

    if (isd) {

    NSLog(@"删除成功");

    }

    else

    {

    NSLog(@"删除失败");

    }

    [fmdb close];

    }

    -(NSMutableArray *)getall

    {

    NSMutableArray *arrm = [NSMutableArray array];

    [fmdb open];

    FMResultSet *set = [[FMResultSet alloc] init];

    set = [fmdb executeQuery:@"select * from classdata order by name desc limit 0,-1"];

    while ([set next]) {

    NSInteger IDs = [set intForColumn:@"ids"];

    NSString *name = [set stringForColumn:@"name"];

    NSString *age = [set stringForColumn:@"age"];

    bok *m = [[bok alloc] init];

    m.IDs = IDs;

    m.name = name;

    m.age = age;

    [arrm addObject:m];

    }

    [fmdb close];

    return arrm;

    }

    @end

    相关文章

      网友评论

          本文标题:FMDB业务处理

          本文链接:https://www.haomeiwen.com/subject/zgjbcxtx.html