FMDB简单练习

先导入第三方
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];
}
本课题设计了一种利用Matlab平台开发的植物叶片健康状态识别方案,重点融合了色彩与纹理双重特征以实现对叶片病害的自动化判别。该系统构建了直观的图形操作界面,便于用户提交叶片影像并快速获得分析结论。Matlab作为具备高效数值计算与数据处理能力的工具,在图像分析与模式分类领域应用广泛,本项目正是借助其功能解决农业病害监测的实际问题。 在色彩特征分析方面,叶片影像的颜色分布常与其生理状态密切相关。通常,健康的叶片呈现绿色,而出现黄化、褐变等异常色彩往往指示病害或虫害的发生。Matlab提供了一系列图像处理函数,例如可通过色彩空间转换与直方图统计来量化颜色属性。通过计算各颜色通道的统计参数(如均值、标准差及主成分等),能够提取具有判别力的色彩特征,从而为不同病害类别的区分提供依据。 纹理特征则用于描述叶片表面的微观结构与形态变化,如病斑、皱缩或裂纹等。Matlab中的灰度共生矩阵计算函数可用于提取对比度、均匀性、相关性等纹理指标。此外,局部二值模式与Gabor滤波等方法也能从多尺度刻画纹理细节,进一步增强病害识别的鲁棒性。 系统的人机交互界面基于Matlab的图形用户界面开发环境实现。用户可通过该界面上传待检图像,系统将自动执行图像预处理、特征抽取与分类判断。采用的分类模型包括支持向量机、决策树等机器学习方法,通过对已标注样本的训练,模型能够依据新图像的特征向量预测其所属的病害类别。 此类课题设计有助于深化对Matlab编程、图像处理技术与模式识别原理的理解。通过完整实现从特征提取到分类决策的流程,学生能够将理论知识与实际应用相结合,提升解决复杂工程问题的能力。总体而言,该叶片病害检测系统涵盖了图像分析、特征融合、分类算法及界面开发等多个技术环节,为学习与掌握基于Matlab的智能检测技术提供了综合性实践案例。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值