桂林dj网站,郑州上海做网站的公司,做木材加工的企业网站首页,西安建设公司都有哪些这周基本完成了知乎日报的内容#xff0c;主要写了点赞收藏的滑动刷新的保存#xff0c;还有收藏文章的功能#xff0c;主要用到FMDB库。
对于如何使用FMDB库
同样需要用cocopads引入FMDB库,使用时需要引入头文件
#import FMDatabase.hFMDB库的使用类似于C语…这周基本完成了知乎日报的内容主要写了点赞收藏的滑动刷新的保存还有收藏文章的功能主要用到FMDB库。
对于如何使用FMDB库
同样需要用cocopads引入FMDB库,使用时需要引入头文件
#import FMDatabase.hFMDB库的使用类似于C语言的文件操作需要我们手动创库之后编译器会生成一个文件用于数据的存储当程序关闭后文件中的内容也不会消失当程序重新启动后会继续利用从而达到本地持久化。
FMDB的创建
- (void)dateBaseInit {NSString* collectionDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString* filename [collectionDoc stringByAppendingPathComponent:collection1.sqlite];self.collectionDataBase [FMDatabase databaseWithPath:filename];if ([self.collectionDataBase open]) {BOOL result [self.collectionDataBase executeUpdate:CREATE TABLE IF NOT EXISTS collectionData (mainLabel text NOT NULL, imageURL text NOT NULL, id text NOT NULL, url text NOT NULL);];if (result) {NSLog(创表成功);} else {NSLog(创表失败);}}NSString *goodDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString * fileName [goodDoc stringByAppendingPathComponent:likes.sqlite];self.likesDataBase [FMDatabase databaseWithPath:fileName];if ([self.likesDataBase open]) {BOOL result [self.likesDataBase executeUpdate:CREATE TABLE IF NOT EXISTS likesData (id text NOT NULL);];if (result) {NSLog(创表成功);} else {NSLog(创表失败);}}
}不必在意其何时创建的只要在你用到它时创建好就行。在程序重新启动时该段代码会在执行一遍但不会创建一个新的库覆盖前面的可以理解为会执行但没有任何效果。
数据的添加删除查询。
- (void)insertLikesData:(NSString*)string {if ([self.likesDataBase open]) {BOOL result [self.likesDataBase executeUpdate:INSERT INTO likesData (id) VALUES (?);, string];if (!result) {NSLog(增加数据失败);} else {NSLog(增加数据成功);}[self.likesDataBase close];}
}- (void)deleteLikesDate:(NSString*)nowID {if ([self.likesDataBase open]) {NSString *sql delete from likesData WHERE id ?;BOOL result [self.likesDataBase executeUpdate:sql, nowID];if (!result) {NSLog(数据删除失败);} else {NSLog(数据删除成功);}[self.likesDataBase close];}
}NSString* collectionDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString* filename [collectionDoc stringByAppendingPathComponent:collection1.sqlite];self.collectionDataBase [FMDatabase databaseWithPath:filename];if ([self.collectionDataBase open]) {FMResultSet* collectionResultSet [self.collectionDataBase executeQuery:SELECT * FROM collectionData];while ([collectionResultSet next]) {NSString* idString [collectionResultSet stringForColumn:id];if ([nowIdSting isEqualToString:idString]) {self.flage1 1;self.keepButton.tag 6;[self.keepButton setImage:[UIImage imageNamed:shoucangxuanzhong-2] forState:UIControlStateNormal];}}[self.collectionDataBase close];}对于在不同的文件下获取到库在开始没有理解其用处以为要将库保存下来并传值那么就没有必要创这个库。以上面的查询为例获取数据库文件的路径获取数据库注意文件的路径必须一样。
NSString* collectionDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString* filename [collectionDoc stringByAppendingPathComponent:collection1.sqlite];self.collectionDataBase [FMDatabase databaseWithPath:filename];对于点赞收藏的滑动刷新
获取该文章的ID遍历数据库判断是否收藏点赞同时刷新控件这里要注意要改变按钮的点击状态这里是改变按钮的tag值
- (void)likesAndCollectReload:(NSString*) nowIdSting{self.flage1 0;self.flage2 0;NSString* collectionDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString* filename [collectionDoc stringByAppendingPathComponent:collection1.sqlite];self.collectionDataBase [FMDatabase databaseWithPath:filename];if ([self.collectionDataBase open]) {FMResultSet* collectionResultSet [self.collectionDataBase executeQuery:SELECT * FROM collectionData];while ([collectionResultSet next]) {NSString* idString [collectionResultSet stringForColumn:id];if ([nowIdSting isEqualToString:idString]) {self.flage1 1;self.keepButton.tag 6;[self.keepButton setImage:[UIImage imageNamed:shoucangxuanzhong-2] forState:UIControlStateNormal];}}[self.collectionDataBase close];}if (self.flage1 0) {self.keepButton.tag 3;[self.keepButton setImage:[UIImage imageNamed:shoucang-2] forState:UIControlStateNormal];}NSString *goodDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString * fileName [goodDoc stringByAppendingPathComponent:likes.sqlite];self.likesDataBase [FMDatabase databaseWithPath:fileName];if ([self.likesDataBase open]) {// 1.执行查询语句FMResultSet *likesResultSet [self.likesDataBase executeQuery:SELECT * FROM likesData];// 2.遍历结果while ([likesResultSet next]) {NSString* idString1 [likesResultSet stringForColumn:id];if ([nowIdSting isEqualToString:idString1]) {self.flage2 1;self.likesButton.tag 4;[self.likesButton setImage:[UIImage imageNamed:a-24geshangchuan-20] forState:UIControlStateNormal];}}[self.likesDataBase close];}if (self.flage2 0) {self.likesButton.tag 2;[self.likesButton setImage:[UIImage imageNamed:good] forState:UIControlStateNormal];}
}对于文章的收藏于取消收藏我们一般将数据库的数据提取到数组中使用在数据库内容发生改变时就需要重新提取。以在文章内容界面取消收藏返回收藏夹更新收藏夹的内容为例子。这里用到通知在返回到收藏夹执行相应的更新方法
[[NSNotificationCenter defaultCenter] postNotificationName:reloadCollectCell object:nil userInfo:nil];[self dismissViewControllerAnimated:YES completion:nil];- (void)reloadCollectCell{[self twoArrayInit];[self.storiesTableView reloadData];
}
- (void)twoArrayInit {self.titleArray [NSMutableArray array];self.imageUrlArray [NSMutableArray array];self.idArray [NSMutableArray array];self.urlArray [NSMutableArray array];NSString* collectionDoc [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString* filename [collectionDoc stringByAppendingPathComponent:collection1.sqlite];self.collectionDataBase [FMDatabase databaseWithPath:filename];if ([self.collectionDataBase open]) {FMResultSet* collectionResultSet [self.collectionDataBase executeQuery:SELECT * FROM collectionData];while ([collectionResultSet next]) {NSString* idString [collectionResultSet stringForColumn:id];[self.idArray addObject:idString];[self.titleArray addObject:[collectionResultSet stringForColumn:mainLabel]];[self.imageUrlArray addObject:[collectionResultSet stringForColumn:imageURL]];[self.urlArray addObject:[collectionResultSet stringForColumn:url]];}[self.collectionDataBase close];}
}