1.導入fmdb庫,封裝數據庫類FMDBManager.h,具體代碼見MyNoteBook->MySources文件夾裏; 2.首頁搜索仿iOS 以前短信頁面,首頁佈局使用UICollectionView,詳情頁使用UITextView控件; 3.業務邏輯和功能很簡單,主要實現了數據庫的增刪改查,很適合入門練手。git
數據庫的增刪改查github
- (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(@"數據插入失敗!");
}
}
複製代碼
- (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(@"數據刪除失敗!");
}
}
複製代碼
- (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(@"數據更新失敗!");
}
}
複製代碼
- (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