前面介绍过FMDB以及SQLITE的基本操作具体参考前面的微博
FMDB:
http://blog.csdn.net/lee727n/article/details/71176162
SQLite:
http://blog.csdn.net/lee727n/article/details/71171120
下面我们通过FMDB来实现一个日常生活经常用到的的通讯录,代码结构如下,首先需要有一个tableview用来展示信息,还需要一个viewController来布局新建联系人的界面。需要一个person类,用于存放每个人的信息,还需要一个DBMnager类,用于存放FMDB所用的建表,增删改查等方法:
1.模型类person的实现
@interface Person : NSObject
@property (nonatomic)int pid;
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *code;
@end
2.DBMnager类的实现
需要FMDatabase类的属性,需要4个方法增删改查。特别注意初始化数据库,以及创建表需要放在单例中实现。这样做的好处,1,生命周期覆盖整个app的生存时间 2,多页面使用数据库时无需传值,直接使用同一份数据。
@property (nonatomic, strong)FMDatabase *db;
+ (DBManager *)shareManager;
- (void)insertPerson:(Person *)person;
- (void)deletePerson:(Person *)person;
- (void)updatePerson:(Person *)person;
- (NSMutableArray *)queryPersons;
单例方法,初始化数据库以及建表
static DBManager *_manager;
@implementation DBManager
+(DBManager *)shareManager{
if (!_manager) {
_manager = [DBManager new];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/persons.db"];
_manager.db = [FMDatabase databaseWithPath:path];
if ([_manager.db open]) {
NSLog(@"打开数据库文件成功");
//注意主键的使用
if ([_manager.db executeUpdate:@"create table if not exists persons (name text,code text, pid integer primary key autoincrement)"]) {
NSLog(@"创建表成功!");
}else NSLog(@"创建表失败");
}
}
return _manager;
}
实现增删改查方法
-(void)insertPerson:(Person *)person{
//插入指定字段,第三个字段pid是主键,自动+1
NSString *sql = [NSString stringWithFormat:@"insert into persons (name, code) values ('%@','%@')",person.name,person.code];
if ([self.db executeUpdate:sql]) {
NSLog(@"插入数据成功!");
}else NSLog(@"插入数据失败");
}
-(void)deletePerson:(Person *)person{
NSString *sql = [NSString stringWithFormat:@"delete from persons where pid=%d",person.pid];
if ([self.db executeUpdate:sql]) {
NSLog(@"删除数据成功!");
}else NSLog(@"删除数据失败!");
}
-(void)updatePerson:(Person *)person{
NSString *sql = [NSString stringWithFormat:@"update persons set name='%@', code='%@' where pid=%d",person.name,person.code,person.pid];
if ([self.db executeUpdate:sql]) {
NSLog(@"修改数据成功!");
}else NSLog(@"修改数据失败!");
}
-(NSMutableArray *)queryPersons{
NSMutableArray *persons = [NSMutableArray array];
FMResultSet *result = [self.db executeQuery:@"select * from persons"];
while ([result next]) {
Person *p = [Person new];
p.name = [result stringForColumn:@"name"];
p.code = [result stringForColumn:@"code"];
p.pid = [result intForColumn:@"pid"];
[persons addObject:p];
}
return persons;
}
新建联系人页面的实现:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"新建联系人";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAction)];
if (self.person) {//修改
self.nameTF.text = self.person.name;
self.codeTF.text = self.person.code;
}
}
保存方法对应的内容
- (void)saveAction {
if (self.person) {//修改
self.person.name = self.nameTF.text;
self.person.code = self.codeTF.text;
[[DBManager shareManager]updatePerson:self.person];
}else{
Person *p = [Person new];
p.name = self.nameTF.text;
p.code = self.codeTF.text;
[[DBManager shareManager]insertPerson:p];
}
[self.navigationController popViewControllerAnimated:YES];
}
tableview界面的实现
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"通讯录";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
}
- (void)addAction {
PersonViewController *vc = [PersonViewController new];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
实现tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
self.persons = [[DBManager shareManager]queryPersons];
return self.persons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Person *p = self.persons[indexPath.row];
cell.textLabel.text = p.name;
cell.detailTextLabel.text = p.code;
return cell;
}
实现侧滑删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Person *p = self.persons[indexPath.row];
[[DBManager shareManager]deletePerson:p];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
实现tableview点击cell事件,通过属性传值,实现修改效果
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Person *p = self.persons[indexPath.row];
PersonViewController *vc = [PersonViewController new];
vc.person = p;
[self.navigationController pushViewController:vc animated:YES];
}
程序源代码请通过github下载:
https://github.com/lee727n/address-book