<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                關于FMDB的講解以下參考文章講得挺不錯,所以直接引用。 [http://www.cocoachina.com/bbs/read.php?tid=140901](http://www.cocoachina.com/bbs/read.php?tid=140901) [http://www.cnblogs.com/wuhenke/archive/2012/02/07/2341656.html](http://www.cnblogs.com/wuhenke/archive/2012/02/07/2341656.html) [http://www.cnblogs.com/wendingding/p/3873874.html](http://www.cnblogs.com/wendingding/p/3873874.html) 用FMDB框架實現上一節的效果。![大笑](https://box.kancloud.cn/2016-01-19_569e21abc5518.gif) ~~~ // // ViewController.m // FMDB // #import "ViewController.h" #import "Shop.h" #import "FMDB.h" @interface ViewController ()<UITableViewDataSource,UISearchBarDelegate> @property (nonatomic,strong) FMDatabase *db; @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *priceField; @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic,strong) NSMutableArray *shops; - (IBAction)addShop; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UISearchBar *searchBar = [[UISearchBar alloc] init]; searchBar.frame = CGRectMake(0, 0, 320, 44); searchBar.delegate = self; self.tableView.tableHeaderView = searchBar; [self initDB]; [self searchShopWithName:@""]; } -(NSMutableArray *)shops{ if (!_shops) { _shops = [NSMutableArray array]; } return _shops; } -(void)initDB{ //打開數據庫 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"shop111.sqlite"]; NSLog(@"%@",path); _db = [FMDatabase databaseWithPath:@""]; [_db open]; //創建表 [_db executeUpdate:@"create table if not exists shop(id integer primary key,name text not null,price real);"]; } - (IBAction)addShop { NSString *name = self.nameField.text; NSString *price = self.priceField.text; //BOOL add = [self.db executeUpdateWithFormat:@"insert into shop(name,price) values(%@,%f)",name,price.doubleValue]; NSString *sql = @"insert into shop(name,price) values(?,?)"; NSArray *parm = @[name,price];//@[@"蘋果",@23]; BOOL add = [self.db executeUpdate:sql withArgumentsInArray:parm]; if (add) { Shop *shop = [[Shop alloc] init]; shop.name = self.nameField.text; shop.price = self.priceField.text; [self.shops addObject:shop]; self.nameField.text = @""; self.priceField.text = @""; [self shoAlertWithMsg:@"新增成功!"]; [self.tableView reloadData]; //long long a = [self.db lastInsertRowId];//獲取最新插入的id //NSLog(@"======%lld",a); }else{ [self shoAlertWithMsg:@"新增失敗!"]; } } -(void)searchShopWithName:(NSString *)name{ [self.shops removeAllObjects]; NSString *sql = [NSString stringWithFormat:@"select * from shop where name like '%%%@%%';",name]; FMResultSet *set = [self.db executeQuery:sql]; //FMResultSet *set = [self.db executeQuery:@"select * from shop"]; while (set.next) { Shop *shop = [[Shop alloc] init]; shop.name = [set stringForColumn:@"name"]; shop.price = [set stringForColumn:@"price"]; [self.shops addObject:shop]; } [set close]; [self.tableView reloadData]; } #pragma mark UITableViewDataSource -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.shops.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"shop"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; cell.backgroundColor = [UIColor grayColor]; } Shop *shop = self.shops[indexPath.row]; cell.textLabel.text = shop.name; cell.detailTextLabel.text = shop.price; cell.detailTextLabel.textColor = [UIColor redColor]; return cell; } #pragma mark UISearchBarDelegate -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self searchShopWithName:searchText]; } -(void)shoAlertWithMsg:(NSString *)msg{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *close = [UIAlertAction actionWithTitle:@"關閉" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:close]; [self presentViewController:alert animated:YES completion:nil]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看