标签:
// // ViewController.m // SQLLITE // // Created by wup on 15/5/26. // Copyright (c) 2015年 apple. All rights reserved. // #import "ViewController.h" #import <sqlite3.h> #import "shop.h" #import "FMDB.h" @interface ViewController () <UITableViewDataSource,UISearchBarDelegate> @property (nonatomic,strong) NSMutableArray *shops; @property (nonatomic,strong) UITableView *tbv ; @property (nonatomic,strong) FMDatabase *fmdb; @end @implementation ViewController -(NSMutableArray *)shops { if (!_shops) { _shops =[NSMutableArray array];//初始化数组 } return _shops; } - (void)viewDidLoad { [super viewDidLoad]; _tbv = [[UITableView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:_tbv]; _tbv.dataSource = self; _tbv.separatorStyle = UITableViewCellSeparatorStyleNone;//去除tableview上面的横线 self.tbv.contentInset = UIEdgeInsetsMake(64, 0, 0 , 0 ); UISearchBar *uscb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)]; [self.view addSubview:uscb]; uscb.delegate = self; NSString *filename = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory , NSUserDomainMask, YES).lastObject stringByAppendingString:@"wup4.sqllite"] ; _fmdb = [[FMDatabase alloc] initWithPath:filename];//初始化sqlite [_fmdb open];//打开数据库 // 建表 [_fmdb executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"]; /** * 数据表插入数据方法 */ for (int i = 0 ;i < 100 ; i ++) { NSString *str = [NSString stringWithFormat:@"手机%d",i]; [_fmdb executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %u);",str,arc4random() % 1000]; } /** * 数据表检索,while循环每次获取到下一行数据时将数据转换成模型并添加到数组中。 * fmres.next返回的事bool类型,获取到为真,检索完成即为假。 */ FMResultSet *fmres = [_fmdb executeQuery:@"SELECT name,price FROM t_shop;"];//数据库查询命令 while (fmres.next) { shop *testshop = [[shop alloc] init]; testshop.name = [fmres stringForColumn:@"name"]; testshop.price = [fmres stringForColumn:@"price"]; [self.shops addObject:testshop]; } NSLog(@"%d",_shops.count); NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)_shops));//ARC下打印retain count // [_fmdb executeUpdate:@"DELETE FROM t_shop;"]; }; #pragma mark - searchbar 的代理方法 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSString *sql = [NSString stringWithFormat:@"SELECT name,price FROM t_shop WHERE name LIKE ‘%%%@%%‘ OR price LIKE ‘%%%@%%‘;",searchText,searchText]; FMResultSet *fmres = [self.fmdb executeQuery:sql];//数据库查询命令 NSLog(@"%d",_shops.count); NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)_shops));//ARC下打印retain count self.shops = nil; while (fmres.next) { shop *testshop = [[shop alloc] init]; testshop.name = [fmres stringForColumn:@"name"]; testshop.price = [fmres stringForColumn:@"price"]; [self.shops addObject:testshop]; } NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)_shops));//ARC下打印retain count [self.tbv reloadData]; } #pragma mark - tableview 的代理方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.shops.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } shop *test2shop = self.shops[indexPath.row]; cell.textLabel.text = test2shop.name; cell.detailTextLabel.text = test2shop.price; return cell; } @end
标签:
原文地址:http://my.oschina.net/wupengnash/blog/420284