FMDB的简单使用

来源:互联网 发布:网络流最短路径增值 编辑:程序博客网 时间:2024/04/27 22:47

简介

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

数据库创建在沙盒路径的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
原创粉丝点击