使用FMDB进行数据库操作

FMDB: https://github.com/ccgus/fmdb

按照教程可以通过pods的方式进行依赖引入。


简单的增删改查操作,代码如下:

  1. #import "ViewController.h"  
  2. #import "FMDB.h"  
  3.   
  4. @interface ViewController ()  
  5. @property(nonatomic,strongFMDatabase *db;  
  6.   
  7. - (IBAction)insert:(UIButton *)sender;  
  8. - (IBAction)delete:(UIButton *)sender;  
  9. - (IBAction)update:(UIButton *)sender;  
  10. - (IBAction)select:(UIButton *)sender;  
  11.   
  12. @end  
  13.   
  14. @implementation ViewController  
  15.   
  16. - (void)viewDidLoad {  
  17.     [super viewDidLoad];  
  18.       
  19.     //数据的路径,放在沙盒的cache下面  
  20.     NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];  
  21.     NSString *filePath = [cacheDir stringByAppendingPathComponent:@"contact.sqlite"];  
  22.       
  23.     //创建并且打开一个数据库  
  24.     _db = [FMDatabase databaseWithPath:filePath];  
  25.       
  26.     BOOL flag = [_db open];  
  27.     if (flag) {  
  28.         NSLog(@"数据库打开成功");  
  29.     }else{  
  30.         NSLog(@"数据库打开失败");  
  31.     }  
  32.       
  33.     //创建表  
  34.    BOOL create =  [_db executeUpdate:@"create table if not exists t_health(id integer primary key  autoincrement, name text,phone text)"];  
  35.       
  36.     if (create) {  
  37.         NSLog(@"创建表成功");  
  38.     }else{  
  39.         NSLog(@"创建表失败");  
  40.     }  
  41.       
  42. }  
  43.   
  44. - (IBAction)insert:(UIButton *)sender {  
  45.       
  46.     BOOL insert = [_db executeUpdate:@"insert into t_health (name,phone) values(?,?)",@"jacob",@"138000000000"];  
  47.     if (insert) {  
  48.         NSLog(@"插入数据成功");  
  49.     }else{  
  50.         NSLog(@"插入数据失败");  
  51.     }  
  52. }  
  53.   
  54. - (IBAction)delete:(UIButton *)sender {  
  55.     BOOL delete = [_db executeUpdate:@"delete from t_health where name like ?",@"jacob"];  
  56.     if (delete) {  
  57.         NSLog(@"删除数据成功");  
  58.     }else{  
  59.         NSLog(@"删除数据失败");  
  60.     }  
  61. }  
  62.   
  63. - (IBAction)update:(UIButton *)sender {  
  64.     BOOL update = [_db executeUpdate:@"update t_health set name = ?  where phone = '%@'",@"jacob111"];  
  65.     if (update) {  
  66.         NSLog(@"更新数据成功");  
  67.     }else{  
  68.         NSLog(@"更新数据失败");  
  69.     }  
  70.       
  71. }  
  72.   
  73. - (IBAction)select:(UIButton *)sender {  
  74.    FMResultSet *set = [_db executeQuery:@"select * from t_health "];  
  75.     while ([set next]) {  
  76.         NSString *name =  [set stringForColumn:@"name"];  
  77.         NSString *phone = [set stringForColumn:@"phone"];  
  78.         NSLog(@"name : %@ phone: %@",name,phone);  
  79.     }  
  80.      
  81. }  
  82. @end 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值