简易记事本-数据库入门一

前言:

由于这个小项目是15年写的,大家不要在意代码质量,请注重本文主题功能-简单数据库的使用。主要功能有数据库增删改查;页面的UI动画(删除时抖动,搜索时导航动画);本地通知使用;模糊搜索;分享;撤销等。
实现步骤:

1.导入fmdb库,封装数据库类FMDBManager.h,具体代码见MyNoteBook->MySources文件夹里; 2.首页搜索仿iOS 之前短信页面,首页布局使用UICollectionView,详情页使用UITextView控件; 3.业务逻辑和功能很简单,主要实现了数据库的增删改查,很适合入门练手。

数据库的增删改查

  • 1.增
- (void)addNewNote:(MyNote *)note {
    BOOL isOpen = [fmDatabase open];
    if (isOpen) {
        NSLog(@"数据库打开成功!");
    } else {
        NSLog(@"数据库打开失败!");
    }

    NSString *sql = @"insert into MyNote(date,content) values(?,?)";
    NSString *date = note.date;
    NSString *newNote = note.content;
    if ([fmDatabase executeUpdate:sql, date, newNote]) {
        NSLog(@"数据插入成功!");
        
        [fmDatabase close];
    } else {
        NSLog(@"数据插入失败!");
    }
}
复制代码
  • 2.删
- (void)deleteNote:(MyNote *)note {
    BOOL isOpen = [fmDatabase open];
    if (isOpen) {
        NSLog(@"数据库打开成功!");
    } else {
        NSLog(@"数据库打开失败!");
    }
    
    NSString *sql = [NSString stringWithFormat:@"delete from MyNote where date = '%@'", note.date];
    if ([fmDatabase executeUpdate:sql]) {
        NSLog(@"数据删除成功!");
        
        [fmDatabase close];
    } else {
        NSLog(@"数据删除失败!");
    }
}
复制代码
  • 3.改
- (void)updateMyNote:(MyNote *)note {
    BOOL isOpen = [fmDatabase open];
    if (isOpen) {
        NSLog(@"数据库打开成功!");
    } else {
        NSLog(@"数据库打开失败!");
    }
    
    NSString *sql = [NSString stringWithFormat:@"update MyNote set content = '%@', date = '%@' where ID = '%zi'", note.content, note.date, note.ID];
    if ([fmDatabase executeUpdate:sql]) {
        NSLog(@"数据更新成功!");
        
        [fmDatabase close];
    } else {
        NSLog(@"数据更新失败!");
    }
}
复制代码
  • 4.查
- (NSArray *)selectNotes {
    BOOL isOpen = [fmDatabase open];
    if (isOpen) {
        NSLog(@"数据库打开成功!");
    } else {
        NSLog(@"数据库打开失败!");
    }
    
    NSString *sql = @"select * from MyNote";
    FMResultSet *set = [fmDatabase executeQuery:sql];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    while ([set next]) {
        MyNote *tmpNote = [[MyNote alloc] init];
        tmpNote.date = [set stringForColumn:@"date"];
        tmpNote.content = [set stringForColumn:@"content"];
        tmpNote.ID = [set intForColumn:@"ID"];

        [array addObject:tmpNote];
    }
    
    [fmDatabase close];
    return array;
}

复制代码

具体步骤请移驾:github下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值