关于FMDB的讲解

     关于FMDB的讲解以下参考文章讲得挺不错,所以直接引用。

http://www.cocoachina.com/bbs/read.php?tid=140901

http://www.cnblogs.com/wuhenke/archive/2012/02/07/2341656.html

http://www.cnblogs.com/wendingding/p/3873874.html


用FMDB框架实现上一节的效果。大笑

[objc]  view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  FMDB  
  4. //  
  5.   
  6. #import "ViewController.h"  
  7. #import "Shop.h"  
  8. #import "FMDB.h"  
  9.   
  10. @interface ViewController ()<UITableViewDataSource,UISearchBarDelegate>  
  11. @property (nonatomic,strongFMDatabase *db;  
  12. @property (weak, nonatomic) IBOutlet UITextField *nameField;  
  13. @property (weak, nonatomic) IBOutlet UITextField *priceField;  
  14. @property (weak, nonatomic) IBOutlet UITableView *tableView;  
  15. @property (nonatomic,strongNSMutableArray *shops;  
  16. - (IBAction)addShop;  
  17.   
  18. @end  
  19.   
  20. @implementation ViewController  
  21.   
  22. - (void)viewDidLoad {  
  23.     [super viewDidLoad];  
  24.     UISearchBar *searchBar = [[UISearchBar alloc] init];  
  25.     searchBar.frame = CGRectMake(0032044);  
  26.     searchBar.delegate = self;  
  27.     self.tableView.tableHeaderView = searchBar;  
  28.       
  29.     [self initDB];  
  30.     [self searchShopWithName:@""];  
  31. }  
  32.   
  33.   
  34.   
  35. -(NSMutableArray *)shops{  
  36.     if (!_shops) {  
  37.         _shops = [NSMutableArray array];  
  38.     }  
  39.     return _shops;  
  40. }  
  41.   
  42. -(void)initDB{  
  43.     //打开数据库  
  44.     NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"shop111.sqlite"];  
  45.     NSLog(@"%@",path);  
  46.     _db = [FMDatabase databaseWithPath:@""];  
  47.     [_db open];  
  48.       
  49.     //创建表  
  50.     [_db executeUpdate:@"create table if not exists shop(id integer primary key,name text not null,price real);"];  
  51. }  
  52.   
  53. - (IBAction)addShop {  
  54.     NSString *name = self.nameField.text;  
  55.     NSString *price = self.priceField.text;  
  56.     //BOOL add = [self.db executeUpdateWithFormat:@"insert into shop(name,price) values(%@,%f)",name,price.doubleValue];  
  57.     NSString *sql = @"insert into shop(name,price) values(?,?)";  
  58.     NSArray *parm = @[name,price];//@[@"苹果",@23];  
  59.     BOOL add = [self.db executeUpdate:sql withArgumentsInArray:parm];  
  60.     if (add) {  
  61.         Shop *shop = [[Shop alloc] init];  
  62.         shop.name = self.nameField.text;  
  63.         shop.price = self.priceField.text;  
  64.         [self.shops addObject:shop];  
  65.         self.nameField.text = @"";  
  66.         self.priceField.text = @"";  
  67.         [self shoAlertWithMsg:@"新增成功!"];  
  68.         [self.tableView reloadData];  
  69.           
  70.         //long long  a = [self.db lastInsertRowId];//获取最新插入的id  
  71.         //NSLog(@"======%lld",a);  
  72.     }else{  
  73.         [self shoAlertWithMsg:@"新增失败!"];  
  74.     }  
  75. }  
  76.   
  77. -(void)searchShopWithName:(NSString *)name{  
  78.     [self.shops removeAllObjects];  
  79.       
  80.     NSString *sql = [NSString stringWithFormat:@"select * from shop where name like '%%%@%%';",name];  
  81.    FMResultSet *set = [self.db executeQuery:sql];  
  82.     //FMResultSet *set = [self.db executeQuery:@"select * from shop"];  
  83.       
  84.     while (set.next) {  
  85.         Shop *shop = [[Shop alloc] init];  
  86.         shop.name = [set stringForColumn:@"name"];  
  87.         shop.price = [set stringForColumn:@"price"];  
  88.         [self.shops addObject:shop];  
  89.     }  
  90.     [set close];  
  91.     [self.tableView reloadData];  
  92. }  
  93.   
  94. #pragma mark UITableViewDataSource  
  95. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  96.     return self.shops.count;  
  97. }  
  98.   
  99. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  100.     static NSString *ID = @"shop";  
  101.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];  
  102.     if (!cell) {  
  103.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];  
  104.         cell.backgroundColor = [UIColor grayColor];  
  105.     }  
  106.       
  107.     Shop *shop = self.shops[indexPath.row];  
  108.     cell.textLabel.text = shop.name;  
  109.     cell.detailTextLabel.text = shop.price;  
  110.     cell.detailTextLabel.textColor = [UIColor redColor];  
  111.       
  112.     return cell;  
  113. }  
  114.   
  115. #pragma mark UISearchBarDelegate  
  116. -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{  
  117.     [self searchShopWithName:searchText];  
  118. }  
  119.   
  120. -(void)shoAlertWithMsg:(NSString *)msg{  
  121.     UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];  
  122.       
  123.     UIAlertAction *close = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:nil];  
  124.       
  125.     [alert addAction:close];  
  126.     [self presentViewController:alert animated:YES completion:nil];  
  127. }  
  128.   
  129. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{  
  130.       
  131. }  
  132.   
  133. - (void)didReceiveMemoryWarning {  
  134.     [super didReceiveMemoryWarning];  
  135.     // Dispose of any resources that can be recreated.  
  136. }  
  137.   
  138.   
  139. @end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值