FMDB 简单使用

导入FMDB第三方库是必须的 添加libsqlite3.tbd库
1.Model类
2.实现
3进行传值
1.
@interface Music : NSObject
@property (nonatomic,assign) NSInteger IDs;
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *singer;
@end
2.
//静态设置
static FMDataBaseC *fmc;
static FMDatabase *fmdb;
@implementation FMDataBaseC
+(FMDataBaseC *)sharedFM{
    if (!fmc) 
    {
        fmc = [[FMDataBaseC alloc]init];
        [fmc initDB];
    }
    return fmc; 
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{

    if (!fmc) {
        fmc = [super allocWithZone:zone];
        
    }
    return fmc;
    
}

-(id)mutableCopy{
    return self;
}

-(id)copy{
    return self;
}

-(void)initDB{
    
    
    NSString *docmentpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docmentpath stringByAppendingPathComponent:@"bys.sqlite"];
    NSLog(@"%@",path);
    
    fmdb = [[FMDatabase alloc]initWithPath:path];
    if ([fmdb open]) {
        [fmdb executeUpdate:@"CREATE TABLE musics(ids INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,singer TEXT)"];
        [fmdb close];
        
    }else{
        NSLog(@"数据表创建失败");
    }
    
    
}

//增插
-(void)insertMusic:(Music *)music{
    
    BOOL isc = false;
    NSArray *arr = [self getAll];
    for (Music *m in arr) {
        
        if ([m.name isEqualToString:music.name] && [m.singer isEqualToString:music.singer]) {
            isc = true;
        }
    }
    if (isc) {
        NSLog(@"数据相同");
    }else{
        [fmdb open];
        BOOL isb = [fmdb executeUpdate:@"insert into musics VALUES(null,?,?)",music.name,music.singer];
        if (isb) {
            NSLog(@"添加成功");
        }else{
            NSLog(@"添加失败");
        }
        [fmdb close];

        
    }
    
    
}

//查询
-(NSMutableArray *)getAll
{
    NSMutableArray *arr = [NSMutableArray array];
    [fmdb open];
    FMResultSet *fmset = [[FMResultSet alloc]init];
    fmset = [fmdb executeQuery:@"select * from musics"];
    while ([fmset next]) {
        NSInteger IDs = [fmset intForColumn:@"ids"];
        NSString *name = [fmset stringForColumn:@"name"];
        NSString *singer = [fmset stringForColumn:@"singer"];
        Music *m = [[Music alloc]init];
        m.IDs = IDs;
        m.name = name;
        m.singer = singer;
        NSLog(@"dis===%ld",IDs);
        [arr addObject:m];
    }
    [fmdb close];
    return arr;
}

//删除
-(void)deleteMusic:(Music *)music{

    [fmdb open];
    NSString * sql=[NSString  stringWithFormat:@"DELETE FROM musics WHERE ids =%ld",music.IDs];
    
    BOOL isb = [fmdb executeUpdate:sql];
    if (isb) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败"); 
    }
    [fmdb close];
}

//更新
-(void)updataMusic:(Music *)music{
    
    [fmdb open];
    NSString *str = [NSString stringWithFormat:@"update musics set singer = '%@' , name = '%@' where ids = %ld",music.singer,music.name,music.IDs];
    
    BOOL isb = [fmdb executeUpdate:str];
    if (isb) {
        NSLog(@"修改成功");
    }else{
        NSLog(@"修改失败");
    }
    [fmdb close];
}


3。
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *table;   //表格式图对象
    NSMutableArray *arr;  //可变数组
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    table.delegate = self;
    table.dataSource =self;
//    table.editing = YES;
//    table.allowsMultipleSelectionDuringEditing = YES;
    [self.view addSubview:table];
    
    UIBarButtonItem *r = [[UIBarButtonItem alloc]initWithTitle:@"+" style:(UIBarButtonItemStyleDone) target:self action:@selector(add)];
    self.navigationItem.rightBarButtonItem = r;
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)add{
    
    //初始化类对象
    AddViewController *add = [[AddViewController alloc]init];
    //跳转
    [self.navigationController pushViewController:add animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    arr = [[FMDataBaseC sharedFM]getAll];
    NSLog(@"%@",arr);
    [table reloadData];
}


//表格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arr.count;
}

//表格单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *ss = @"fdas";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ss];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:ss];
    }
    Music *music = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = music.name;
    cell.detailTextLabel.text = music.singer;
    return cell;
    
    
}

//侧滑删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    Music *m = [arr objectAtIndex:indexPath.row];
    [[FMDataBaseC sharedFM]deleteMusic:m];
    [arr removeObjectAtIndex:indexPath.row];
    
    //    [table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];
    [table reloadData];
}


//选中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UpdataViewController *up = [[UpdataViewController alloc]init];
    up.m = [arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:up animated:YES];
    
}




转载于:https://my.oschina.net/u/2549183/blog/549524

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值