IOS开发基础:FMDB框架的使用

工具 :

代码:

  • ViewController.m
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@property(nonatomic,strong)FMDatabase *db;
-(IBAction)insert;
- (IBAction)update;
- (IBAction)delete;
- (IBAction)query;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student.sqlite"];
    NSLog(@"%@",path);
    self.db=[FMDatabase databaseWithPath:path];
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
        BOOL result=[self.db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"];
        if (result) {
            NSLog(@"建表成功");
        }else
        {
            NSLog(@"建表失败");
        }
    }else{
        NSLog(@"数据库打开失败");    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)insert{
    for (int i=0; i<40; i++) {
        NSString *name=[NSString stringWithFormat:@"rose-%d",arc4random()%1000];
        NSNumber *age=@(arc4random()%100+1);
        [self.db executeUpdate:@"insert into t_student(name,age)values(?,?);",name,age];
    }

}
- (IBAction)update {
    [self.db executeUpdate:@"update t_student set age=? where name=?;",@20,@"jack"];
}

- (IBAction)delete {
    [self.db executeUpdate:@"delete from t_student where age>?;",@60];
}
- (IBAction)query {
    FMResultSet *rs=[self.db executeQuery:@"select * from t_student where age>?;",@50];
    while (rs.next) {
        int ID=[rs intForColumn:@"id"];
        NSString *name=[rs stringForColumn:@"name"];
        int age=[rs intForColumn:@"age"];
        NSLog(@"%d %@ %d",ID,name,age);
    }
}



@end

运行

  • 在运行结果中会显示出路径,找到student.sqlite
    -
  • 打开student.sqlite
    这里写图片描述
  • 点击模拟器中的插入、更改、删除、查询,查看student.sqlite中数据的变化
    这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值