簡易記事本-數據庫入門一

效果圖

前言:

因爲這個小項目是15年寫的,你們不要在乎代碼質量,請注重本文主題功能-簡單數據庫的使用。主要功能有數據庫增刪改查;頁面的UI動畫(刪除時抖動,搜索時導航動畫);本地通知使用;模糊搜索;分享;撤銷等。
實現步驟:

1.導入fmdb庫,封裝數據庫類FMDBManager.h,具體代碼見MyNoteBook->MySources文件夾裏; 2.首頁搜索仿iOS 以前短信頁面,首頁佈局使用UICollectionView,詳情頁使用UITextView控件; 3.業務邏輯和功能很簡單,主要實現了數據庫的增刪改查,很適合入門練手。git

數據庫的增刪改查github

  • 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下載地址sql

相關文章
相關標籤/搜索