FMDB的简单使用

简介

数据持久性存储 增删改查操作 必须先有数据库和表格

数据库创建在沙盒路径的Documents文件夹下

数据库的后缀名一般可以是 .db 或者 .sqlite

数据库要想使用必须确保数据库处于打开的状态

数据库中的同名表格只能创建一个 


1、创建数据库以及创建表格

//<1>在沙盒路径下创建数据库 NSHomeDirectory()

    NSString * path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/PersonDB.db"];

//<2>打开数据库

    fmdb = [[FMDatabase alloc]initWithPath:path];

    BOOL isOpen = [fmdb open];

//<3>判断数据库是否打开成功

    if(isOpen)

    {

        NSLog(@"数据库打开成功");

//<4>创建表格

        NSString * sql = @"create table if not exists Student(ID integer primary key autoincrement,name varchar(256),age integer,image blob)";

        //blob表示二进制 也就是本地的图片保存在数据库的表格中是以NSData形式存放的

        

        BOOL isSuccess = [fmdb executeUpdate:sql];

        //executeUpdate:这个方法 能创建表格、能进行增删改操作 都用同一个方法 只是sql语句不同

        if(isSuccess)

        {

            NSLog(@"表格创建成功");

        }

        else

        {

            NSLog(@"表格创建失败%@",fmdb.lastErrorMessage);

        }

    }

    else

    {

        NSLog(@"数据库打开失败");

    }


2、插入数据

    //<1>sql语句

    NSString * sql = @"insert into Student(name,age,image) values (?,?,?)";

    //?表示的是占位符

    //表格中只能存放对象指针 不能存放基本类型数据

    NSString * name = @"MingGe";

    NSNumber * age = @(18);

    

    //图片存放的是二进制格式

    UIImage * image = [UIImage imageNamed:@"010.png"];

    NSData * imageData = UIImagePNGRepresentation(image);

    

    //<2>插入数据

    BOOL isSuccess = [fmdb executeUpdate:sql,name,age,imageData];

    if(isSuccess)

    {

        NSLog(@"数据插入成功");

    }

    else

    {

        NSLog(@"数据插入失败%@",fmdb.lastErrorMessage);

    }

    //同一个路径下创建同名数据库或者同名表格是不成功 会直接打开同名的数据库或者表格


3、删除数据

    NSString * sql = @"delete from Student where name = ?";

    BOOL isSuccess = [fmdb executeUpdate:sql,@"MingGe"];

    if(isSuccess)

    {

        NSLog(@"删除成功");

    }

    else

    {

        NSLog(@"删除失败%@",fmdb.lastErrorMessage);

    }


4、数据修改

    NSString * sql = @"update Student set age = ?,image = ? where name = ?";

    BOOL isSuceess = [fmdb executeUpdate:sql,@(30),UIImagePNGRepresentation([UIImage imageNamed:@"011.png"]),@"明哥"];

    if(isSuceess)

    {

        NSLog(@"数据修改成功");

    }

    else

    {

        NSLog(@"数据修改失败%@",fmdb.lastErrorMessage);

    }


5、数据查询

    //<1>sql语句

    NSString * sql = @"select * from Student where name = ?";

    //<2>开始查询

    FMResultSet * result = [fmdb executeQuery:sql,@"MingGe"];

    //<3>查询结果显示在UI

    //获取表格中所有的对象信息 使用while循环遍历

    //如果查询满足条件的某一个信息 使用if判断

    if([result next])

    {

        NSString * name = [result stringForColumn:@"name"];

        int age = [result intForColumn:@"age"];

        

        NSData * data = [result dataForColumn:@"image"];

        UIImage *image = [UIImage imageWithData:data];

        

        self.nameLbl.text = name;

        self.ageLbl.text = [NSString stringWithFormat:@"%d",age];

        self.imageView.image = image;

    }

    else

    {

        self.nameLbl.text = @"";

        self.ageLbl.text = @"";

        self.imageView.image = nil;

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值