iOS FMDB的简单使用-代码举例

#import "RootViewController.h"

#import "FMDatabase.h"

@interfaceRootViewController (){

   FMDatabase *_dataBase; 

}

@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    NSLog(@"%@",NSHomeDirectory());

    // Do any additional setup after loading the view.

    [selfcreatButtons];

}

-(void)creatButtons{


    NSArray *titleArray=@[@"创建数据库",@"创建表",@"增加",@"改动",@"查询",@"删除"];

   for (int i=0; i<titleArray.count; i++) {

        UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeSystem];

        [btn setTitle:titleArray[i]forState:UIControlStateNormal];

        btn.frame=CGRectMake((i/3)*100+40, (i%3)*100+40,100,30);

        btn.tag=100+i;

        [btn addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

        [self.viewaddSubview:btn];

    }

}

-(void)click:(UIButton *)button{


   if (button.tag==100) {

       //创建数据库,并打开数据库(因为要创建表)

       //通常情况下,数据库是存储在手机沙盒目录下的,所以要首先找到沙盒路径

        NSString *sandBoxPath=[NSHomeDirectory()stringByAppendingPathComponent:@"Documents/zhulei.db"];//名字自己起

       //根据这个路径,创建数据库

       _dataBase=[[FMDatabasealloc]initWithPath:sandBoxPath];

       //打开数据库

        //[_dataBase open];

       BOOL open=[_dataBaseopen];

       if (open) {

           NSLog(@"数据库创建成功");

        }

    }elseif (button.tag==101){

    

        //创建数据库下的表

       //表里面存在一个主键和要储存的数据的属性

        //创建表的sql语句

        //students :表名

        //username varchar(256),password varchar(256),sex varchar(256) :属性及大小

        NSString *creatTableString=@"create table if not exists students(id integer primary key autoincrement,username varchar(256),password varchar(256),sex varchar(256))";

        //看表单是否创建成功

       BOOL table=[_dataBaseexecuteUpdate:creatTableString];

       if (table) {

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

        }

    }elseif (button.tag==102){

    

        //students里面增加数据

        //insertsql语句

        NSString *insertStr=@"insert into students(username,password,sex) values(?,?,?)";

       BOOL insert=[_dataBaseexecuteUpdate:insertStr,@"朱XXX",@"123456",@""];

       if (insert) {

           NSLog(@"插入成功");

        }

    }elseif (button.tag==104){

    

       //查询数据

        //查询的SQL语句 select

        //表中的id是表自动生成的

        NSString *selectStr=@"select *from students where id=1";

        //查询要从另外一个里面去查 FMResultSet

        //FMResultSet -->可以理解成一个数组

       FMResultSet *set=[_dataBaseexecuteQuery:selectStr];

        //遍历,查询到id=1的那条数据

       while ([setnext]) {

           NSString *username=[setstringForColumn:@"username"];

           NSString *password=[setstringForColumn:@"password"];

           NSString *sex=[setstringForColumn:@"sex"];

           NSLog(@"username=%@ password=%@ sex=%@",username,password,sex);

        }

    }elseif (button.tag==103){

    

        //改动数据 update

        //改动的SQL语句

        NSString *updateStr=@"update students set username='马XXX',password='1234',sex='woman' where id=1";

       BOOL updata=[_dataBaseexecuteUpdate:updateStr];

       if (updata) {

           NSLog(@"改动成功");

        }

    

    }elseif (button.tag==105){

    

        //删除delete

        //删除的SQL语句

        NSString *deleteStr=@"delete from students where id=1";

       BOOL delete=[_dataBaseexecuteUpdate:deleteStr];

       if (delete) {

           NSLog(@"删除成功");

        }

    }

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值