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