FMDB-应用1
#import "ViewController.h"
#import "MyCell.h"
#import "Model.h"
#import "FMDatabase.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableV;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) FMDatabase *dataBase;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建表 -- 打开表
[self createSQTable];
self.dataArr = [[NSMutableArray alloc]init];
for (int i = 0; i < 100; i++) {
Model *mo = [[Model alloc]init];
mo.strId = [NSString stringWithFormat:@"%d",i];
mo.title = [NSString stringWithFormat:@"假数据-标题-%d",i];
mo.content = [NSString stringWithFormat:@"假数据-内容-%d",i+100];
[self.dataArr addObject:mo];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
Model *mo = self.dataArr[indexPath.row];
cell.titleLabel.text = mo.title;
cell.iconImage.image = [UIImage imageNamed:@"share_btn_lol_friend_img_icon_press"];
cell.contentLabel.text = mo.content;
//做判断 数据库里有这个条数据/这个id 对应的颜色发生改变
BOOL isChange = [self resultData:mo.strId];
if (isChange) {
cell.titleLabel.textColor = [UIColor lightGrayColor];
cell.contentLabel.textColor = [UIColor lightGrayColor];
}else{
cell.titleLabel.textColor = [UIColor blackColor];
cell.contentLabel.textColor = [UIColor blackColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//把点击的cell上面的数据记录下来
Model *mo = self.dataArr[indexPath.row];
[self insertData:mo.strId];
//刷新具体某些行或具体某一行
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//整个表格刷新
// [tableView reloadData];
//刷新某些区
// [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath] withRowAnimation:0];
}
//创建SQ表
- (void)createSQTable{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/sq.db"];
self.dataBase = [[FMDatabase alloc]initWithPath:path];
if ([self.dataBase open]) {
NSString *createSq = @"create table if not exists myTable(id text)";
if ([self.dataBase executeUpdate:createSq]) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败:%@",self.dataBase.lastErrorMessage);
}
}
}
//插入数据
- (void)insertData:(NSString *)str{
//插入之前判断数据是否存在/已经存储
if ([self resultData:str]) {
NSLog(@"数据以存储");
}else{
//如果不存在 就插入数据
NSString *insertSq = @"insert into myTable (id) values(?)";
if ([self.dataBase executeUpdate:insertSq,str]) {
NSLog(@"插入成功");
}else{
NSLog(@"插入失败:%@",self.dataBase.lastErrorMessage);
}
}
}
- (BOOL)resultData:(NSString *)str{
//查询具体某一条数据是否存在
NSString *resultSq= @"select *from myTable where id=?";
FMResultSet *set = [self.dataBase executeQuery:resultSq,str];
//查到了返回YES 查不到返回NO
return [set next];
}
@end
FMDB-应用.png
FMDB-应用2----xiaoqiang/卢强/网络阶段/网络\ --1566/Day5/4.FMDB-应用2数据库存储/FMDB-应用2
网友评论