先导入第三方
FMDB
MJExtension
AFNetworking
Header.h
#import "ViewController.h"
#import "FMDB.h"
#import "MJExtension.h"
#import "AFNetworking.h"
model.h
//主键id必须写
@property(nonatomic , assign)NSInteger classid;
//属性
@property(nonatomic , strong)NSString *city,*detail;
model.m
//防崩溃
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
SqliData.h
static SqliData *sqlData1;
static FMDatabase *db;
//单利方法
+(instancetype)initData;
//初始化数据库
-(void)initSql;
//初始化表格
-(void)initTable;
//添加数据
-(void)addData:(WeiModel *)data;
//修改数据
-(void)upData:(WeiModel *)data;
//删除数据
-(void)deleteData:(NSInteger )theid;
//查询数据
-(NSMutableArray *)array;
//关闭数据库
-(void)closeSql;
SqliData.m
static SqliData *sqlData1;
static FMDatabase *db;
//单利方法
+(instancetype)initData{
if (!sqlData1) {
sqlData1 = [[SqliData alloc] init];
}
return sqlData1;
}
//初始化数据库
-(void)initSql{
//获取DOcuments目录
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接路径
NSString *fileName = [str stringByAppendingString:@"/yml.db"];
//创建数据库
db = [[FMDatabase alloc] initWithPath:fileName];
if ([db open]) {
NSLog(@"数据库打开成功");
//调用初始化表格方法
[self initTable];
}else{
NSLog(@"数据库打开失败");
}
}
//初始化表格
-(void)initTable{
//初始化数据库表格的格式:create table if not exists 表名(主键id integer primary key,所有的数据类型);
[db executeUpdate:@"create table WeiModel(classid integer primary key,city text,detail text)"];
//关闭数据库
[db close];
}
//添加数据
-(void)addData:(WeiModel *)data{
if ([db open]) {
//添加数据的sql语句:insert into 表名 values(null,?,?);
[db executeUpdate:[NSString stringWithFormat:@"insert into WeiModel values(null,'%@','%@')",data.city,data.detail]];
}else{
NSLog(@"添加数据失败");
}
//关闭数据库
[db close];
}
//删除数据
-(void)deleteData:(NSInteger)theid{
//sql 语句: delete from 表名 where 表名的主键id = ?
if ([db open]) {
[db executeUpdate:[NSString stringWithFormat:@"delete from WeiModel where classid = '%ld'",theid]];
}else{
NSLog(@"删除数据失败");
}
//关闭数据库
[db close];
}
//修改数据
-(void)upData:(WeiModel *)data{
//sql 语句的格式:update 表名 set 所有数据 where 主键id = ?
if ([db open]) {
[db executeUpdate:[NSString stringWithFormat:@"update WeiModel set city = '%@' ,detail = '%@' where classid = '%ld'",data.city,data.detail,data.classid]];
}else{
NSLog(@"修改数据失败");
}
//关闭数据库
[db close];
}
//查询数据
-(NSMutableArray *)array{
//创建数据
NSMutableArray *arr = [NSMutableArray array];
//集合
FMResultSet *set = [FMResultSet new];
if ([db open]) {
//sql 语句格式:select *from 表名
set = [db executeQuery:@"select *from WeiModel"];
// 判断有没有查询到 一行一行去查询
while ([set next]) {
WeiModel *msg = [[WeiModel alloc] init];
msg.classid = [set intForColumn:@"classid"];
msg.city = [set stringForColumn:@"city"];
msg.detail = [set stringForColumn:@"detail"];
//将msg对象添加到数据
[arr addObject:msg];
}
}else{
NSLog(@"查询失败");
}
[db close];
return arr;
}
//关闭数据库
-(void)closeSql{
[db close];
}
cell.h
//xib拖线
@property (weak, nonatomic) IBOutlet UILabel *aLabel;
@property (weak, nonatomic) IBOutlet UILabel *bLabel;
-(void)loadData:(WeiModel *)Wei;
cell.h
- (void)loadData:(WeiModel *)Wei{
self.aLabel.text = Wei.city;
self.bLabel.text = Wei.detail;
}
VC.M
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *tbv;
NSMutableArray *arr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"FMDB";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(ShouCang)];
tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tbv.delegate = self;
tbv.dataSource = self;
[self.view addSubview:tbv];
[tbv registerNib:[UINib nibWithNibName:@"WeiTableViewCell" bundle:nil] forCellReuseIdentifier:@"WeiTableViewCell"];
[self AFN];
}
-(void)AFN{
//初始化一个AFHTTPSessionManager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://v.juhe.cn/wzpoints/query?key=6d96ee265cc090b418ee51257ff9c48f&lat=40.041478&lon=116.300267" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
self->arr = [WeiModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"list"]];
[self->tbv reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
-(void)ShouCang{
[self.navigationController pushViewController:[OneViewController new] animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WeiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WeiTableViewCell"];
WeiModel *mod = [WeiModel new];
mod = arr[indexPath.row];
[cell loadData:mod];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
WeiModel *mod = arr[indexPath.row];
[[SqliData initData ]initSql];
[[SqliData initData]addData:mod];
}
OneVC.h
//继承UITableViewController
@interface OneViewController : UITableViewController
OneVC.M
@interface OneViewController ()
{
NSMutableArray *arr;
}
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"WeiTableViewCell" bundle:nil] forCellReuseIdentifier:@"WeiTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WeiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WeiTableViewCell"];
WeiModel *mod = [WeiModel new];
mod = arr[indexPath.row];
[cell loadData:mod];
return cell;
}
- (void)viewWillAppear:(BOOL)animated{
[[SqliData initData]initSql];
arr = [[SqliData initData]array];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[[SqliData initData]initSql];
[[SqliData initData]deleteData:[arr[indexPath.row]classid]];
[arr removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}