码迷,mamicode.com
首页 > 数据库 > 详细

FMDB

时间:2016-05-31 23:54:29      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

FMDB是将原生的sqlite数据库封装的库,使用起来比较简单,并且FMDB不支持多个线程同时操作,所以一般以串行的方式实现相关的操作。下面以一个简单通讯录的工程来介绍其简单应用:

功能介绍:主界面显示本地数据库中存储的联系人,点击右按钮,实现页面跳转,跳转到添加联系人页面,在添加联系人页面输入联系人姓名和电话,再点击完成将 添加的联系人写入本地数据库,然后跳转到主界面,并刷新主界面,将本地数据库中的所有联系人进行显示。另,在主界面还带有侧滑删除功能。

首先使用CocoaPods引入第三方:FMDB,再引入libsqlite3.0.tbd。其中,libsqlite3.0.tbd的引入方法与Sqlite数据库的引入方法完全相同。如图,

技术分享

 技术分享

 

AppDelegate.m中的代码:

1   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
2     self.window.backgroundColor = [UIColor whiteColor];
3     [self.window makeKeyAndVisible];
4     
5     UINavigationController *Nav = [[UINavigationController alloc] initWithRootViewController:[[ListTableViewController alloc] initWithStyle:UITableViewStylePlain]];
6     self.window.rootViewController = Nav;

DataHandle.h(封装的操作数据库的类)

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface DataHandle : NSObject
 4 
 5 //单例
 6 + (instancetype)sharedDataHandle;
 7 
 8 //创建表
 9 - (void)createTable;
10 
11 //添加联系人
12 - (void)addContactsWithName:(NSString *)name telephone:(NSString *)telephone;
13 
14 //删除联系人
15 - (void)deleteContects:(NSString *)sender;
16 
17 //查询显示所有联系人
18 - (NSMutableArray *)searchAllContacts;
19 
20 @end

DataHandle.m中的代码:

  1 #import "DataHandle.h"
  2 #import <FMDB.h>
  3 #import <sqlite3.h>
  4 #import "ContactModel.h"
  5 
  6 @interface DataHandle ()
  7 //数据库对象
  8 @property (nonatomic, strong) FMDatabase *dataBase;
  9 //表路径
 10 @property (nonatomic, strong) NSString *filePath;
 11 
 12 @end
 13 
 14 @implementation DataHandle
 15 
 16 //单例
 17 + (instancetype)sharedDataHandle {
 18     static DataHandle *dataHandle = nil;
 19     static dispatch_once_t onceToken = 0;
 20     dispatch_once(&onceToken, ^{
 21         dataHandle = [[DataHandle alloc] init];
 22     });
 23     return dataHandle;
 24 }
 25 //懒加载
 26 - (FMDatabase *)dataBase {
 27     if (_dataBase == nil) {
 28         NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 29         self.filePath = [documentsPath stringByAppendingPathComponent:@"contacts.sqlite"];
 30         self.dataBase = [FMDatabase databaseWithPath:self.filePath];
 31         NSLog(@"%@", self.filePath);
 32 
 33     }
 34     return _dataBase;
 35 }
 36 
 37 //创建表
 38 - (void)createTable {
 39        if ([self.dataBase open]) {
 40         BOOL result = [self.dataBase executeUpdate:@"create table if not exists contacts(ID integer primary key autoincrement not null, name text not null, telephone text not null)"];
 41         
 42         if (result) {
 43             NSLog(@"创建表成功");
 44             
 45         }else{
 46             NSLog(@"创建表失败");
 47         }
 48     }
 49     [self.dataBase close];
 50     
 51 }
 52 //添加联系人
 53 - (void)addContactsWithName:(NSString *)name telephone:(NSString *)telephone {
 54     [self.dataBase open];
 55     //使用队列添加联系人
 56     FMDatabaseQueue *queue = [[FMDatabaseQueue alloc] initWithPath:self.filePath];
 57     __block BOOL isSucceed = YES;
 58     [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
 59        
 60         isSucceed = [db executeUpdate:@"insert into contacts (name,telephone)values(?,?)", name,telephone] && isSucceed;
 61         if (!isSucceed) {
 62             *rollback = YES;
 63             return;
 64         }
 65     }];
 66     [self.dataBase close];
 67 }
 68 
 69 //查询显示所有联系人
 70 - (NSMutableArray *)searchAllContacts {
 71     NSMutableArray *array = [NSMutableArray array];
 72     [self.dataBase open];
 73     //查询结果使用的类FMResultSet
 74     FMResultSet *resultSet = [self.dataBase executeQuery:@"select * from contacts"];
 75     //遍历出需要的结果内容
 76     while ([resultSet next]) {
 77         NSString *name = [resultSet objectForColumnName:@"name"];
 78         NSString *telephone = [resultSet objectForColumnName:@"telephone"];
 79         ContactModel *model = [[ContactModel alloc] init];
 80         model.name = name;
 81         model.telephone = telephone;
 82         [array addObject:model];
 83     }
 84     [self.dataBase close];
 85     return array;
 86 }
 87 
 88 //删除联系人
 89 - (void)deleteContects:(NSString *)sender {
 90     
 91     [self.dataBase open];
 92     BOOL result = [self.dataBase executeUpdate:@"delete from contacts where name = ?", sender];
 93     if (result) {
 94         NSLog(@"删除成功");
 95     }else {
 96         NSLog(@"删除失败");
 97     }
 98     [self.dataBase close];
 99     
100 }

ContactModel.h 模型:

1 #import <Foundation/Foundation.h>
2 
3 @interface ContactModel : NSObject
4 
5 @property (nonatomic, copy) NSString *name;
6 @property (nonatomic, copy) NSString *telephone;
7 
8 @end

ListTableViewController.m 主界面

 1 #import "ListTableViewController.h"
 2 #import "DetailViewController.h"
 3 #import "DataHandle.h"
 4 #import "ContactModel.h"
 5 #import "ListTableViewCell.h"
 6 
 7 @interface ListTableViewController ()
 8 
 9 @property (nonatomic, strong) DataHandle *dataHandle;
10 ///用于接收数据库中取出的数据
11 @property (nonatomic, strong) NSMutableArray *allContactsArray;
12 
13 @end
14 
15 @implementation ListTableViewController
16 //懒加载
17 - (NSMutableArray *)allContactsArray {
18     if (_allContactsArray == nil) {
19         _allContactsArray = [NSMutableArray array];
20     }
21     return _allContactsArray;
22 }
23 
24 - (void)viewDidLoad {
25     [super viewDidLoad];
26     //添加导航栏右按钮
27     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightAction)];
28     [self.tableView registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:nil] forCellReuseIdentifier:@"listCell"];
29     [self loadData];
30     
31 }
32 
33 #pragma mark - 从数据库中取出数据显示在tableView上
34 - (void)loadData {
35     self.dataHandle = [DataHandle sharedDataHandle];
36    self.allContactsArray = [self.dataHandle searchAllContacts];
37     [self.tableView reloadData];
38 }
39 #pragma mark - 右按钮点击事件
40 - (void)rightAction {
41    
42     //点击导航栏右按钮跳转到添加联系人界面
43     DetailViewController *detailVC = [[DetailViewController alloc] init];
44     //block的实现(刷新tableView)
45     __weak typeof(self)weakSelf = self;
46     detailVC.Myblock = ^(){
47         [weakSelf loadData];
48     };
49     [self.navigationController pushViewController:detailVC animated:YES];
50 }
51 
52 
53 #pragma mark - Table view data source
54 //section
55 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56 
57     return 1;
58 }
59 //row
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
61 
62     return self.allContactsArray.count;
63 }
64 
65 //cell
66 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
67     ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];
68     ContactModel *model = self.allContactsArray[indexPath.row];
69     cell.nameLabel.text = model.name;
70     cell.telephoneLabel.text = model.telephone;
71     return cell;
72 }
73 
74 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
75     return 80;
76 }
77 
78 // Override to support editing the table view.//侧滑删除功能将本地数据库中相应的内容删除,再将self.allContactsArray中的内容删除,最后更新UI
79 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
80     if (editingStyle == UITableViewCellEditingStyleDelete) {
81         ContactModel *model = self.allContactsArray[indexPath.row];
82         self.dataHandle = [DataHandle sharedDataHandle];
83         [self.dataHandle deleteContects:model.name];
84         [self.allContactsArray removeObjectAtIndex:indexPath.row];
85     
86         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
87     } else if (editingStyle == UITableViewCellEditingStyleInsert) {
88         
89     }   
90 }
91 
92 @end

DetailViewController.h 添加联系人界面:

1 #import <UIKit/UIKit.h>
2 
3 typedef void(^block)();//定义block方法,实现回调刷新tableView
4 @interface DetailViewController : UIViewController
5 
6 @property (nonatomic, copy) block Myblock;
7 
8 @end

DetailViewController.m 添加联系人界面:使用xib搭建的界面

 1 #import "DetailViewController.h"
 2 #import "DataHandle.h"
 3 #import "ListTableViewController.h"
 4 
 5 
 6 @interface DetailViewController ()
 7 ///联系人姓名
 8 @property (weak, nonatomic) IBOutlet UITextField *nameField;
 9 ///电话号码
10 @property (weak, nonatomic) IBOutlet UITextField *telephoneField;
11 ///封装的数据库
12 @property (nonatomic, strong) DataHandle *dataHandle;
13 
14 @end
15 
16 @implementation DetailViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     //添加导航栏右按钮
21     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction)];
22     self.dataHandle = [DataHandle sharedDataHandle];
23     [self.dataHandle createTable];
24     
25 }
26 #pragma mark - 导航栏右按钮响应方法
27 - (void)doneAction {
28     
29     //self.Myblock(self.nameField.text, self.telephoneField.text);
30     [self.dataHandle addContactsWithName:self.nameField.text telephone:self.telephoneField.text];
31     self.Myblock();//触发block,实现ListTableViewController页面的tableView刷新
32     [self.navigationController popViewControllerAnimated:YES];
33     
34 }
35 
36 @end

ListTableViewCell.h自定义cell使用的xib搭建的界面:

1 #import <UIKit/UIKit.h>
2 
3 @interface ListTableViewCell : UITableViewCell
4 
5 @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
6 
7 @property (weak, nonatomic) IBOutlet UILabel *telephoneLabel;
8 
9 @end

 

FMDB

标签:

原文地址:http://www.cnblogs.com/bdlfbj/p/5547776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!