关于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框架实现上一节的效果。
- //
- // 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