哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般了,代码可能比较乱,希望不要伤了各位的眼睛。依旧是纯代码实现的,没有用到任何IB,其中添加删除更改的操作都非常简单,不需要做太多操作,只需要用到FMDB封装好的executeUpdate方法就行了。搜索功能用到了UISearchDisplayController这个控件,因为以前没有用过,研究了一天才搞定。下面对界面做简单的说明:
整个界面使用一个UITabBarController实现的,一共有三个TabBarItem,第一个是通讯录,我重写了cell,在每一行可以显示姓名,电话和ID。我要说明一下ID,ID最好每个人的都不要写成一样的,如果两个人的姓名一样,ID也一样的话,在删除一个人的时候会把两个人的信息都删掉。右上角是更新button,先选择某一项,再点那个button就可以进入更新界面。下面来看更新界面:
进入更新界面,可以更新姓名和电话,ID是不可更改的,为什么呢,因为在操作数据库的时候必须要有一个主键(PRIMARY KEY),作为区分每条数据的标识。注意右上角是save。下面来看添加信息的页面,也就是第二个TabBarItem:
可以添加姓名,电话和ID,注意ID不要和其他的相同!再来看第三个TabBarItem,搜索功能,这是我新学的一个知识,用UISearchDisplayController和UISearchBar来实现的搜索,因为用这两个可以达到最好的图像效果
搜索出来的数据点击某一行可以进入详情:
这个页面就简单了,没什么可说的了,一目了然。
下面为大家呈上代码,首先要在AppDelegate.m文件里做修改,要放入一个UITabBar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
}
@property(retain,nonatomic)NSString *dbpath;
@property(retain,nonatomic)NSString *nameStr;
@property(retain,nonatomic)NSString *phoneStr;
@property(retain,nonatomic)NSString *IDsStr;
@end
复制代码
UserDetailInfo.m
复制代码
//
// UserDetailInfo.m
// tabbartest
//
// Created by changjian on 12-12-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "UserDetailInfo.h"
#import "FMDatabase.h"
@implementation UserDetailInfo
@synthesize dbpath;
@synthesize nameStr,phoneStr,IDsStr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"详细信息";
NSArray *array = [NSArray arrayWithObjects:@"姓名:",@"电话:",@"ID:", nil];
NSArray *array2 = [NSArray arrayWithObjects:self.nameStr,self.phoneStr,self.IDsStr, nil];
for (int i = 0; i < 3 ; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake( 70,i * 40 + 34, 100, 30)];
label.text = [array objectAtIndex:i];
[self.view addSubview:label];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(140, i * 40 +35, 100, 30)];
label2.text = [array2 objectAtIndex:i];
[self.view addSubview:label2];
[label release];
[label2 release];
}
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *path = [document stringByAppendingPathComponent:@"USER.sqlite"];
self.dbpath = path;
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(deleteFromDatabase)];
self.navigationItem.rightBarButtonItem = deleteButton;
}
- (void)deleteFromDatabase//从数据库删除信息
{
FMDatabase *db = [FMDatabase databaseWithPath:self.dbpath];
NSString *mes = nil;
if ([db open]) {
NSString *sql = @"DELETE FROM USER WHERE name = ? and phone = ? and idcode = ?";
if (self.nameStr.length != 0&&self.phoneStr.length != 0&&self.IDsStr.length !=0){
BOOL rs = [db executeUpdate:sql,self.nameStr,self.phoneStr,self.IDsStr]; //后面跟的三个参数就是sql语句里的三个问号对应
if (rs) {
mes = @"删除成功";
}else{
mes = @"删除失败";
}
}
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:mes delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
alert.delegate = self;//别忘了代理
[alert show];
[alert release];
[db close];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"点击了删除成功");
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end