第一步:下载第三方库,点击 连接 下载,

第二部:准备数据库:按照连接&中博客的步骤实现数据库,

数据库的设计大致如下表:

id        username             password         registertime      realname            groupid             

 
   

新建一个单视图工程,

关闭arc  (Automatic reference count)自动引用计数,

添加 sqlite3.dylib 类库的包含,

将准备好的第三方库FMDB 以及准备好的数据库包含在项目中,包含时要注意选中copy 选项,不然只是单纯的引用到项目中,

包含后项目结构如下,

单击打开 wsqViewController.xib  拖放两个UILable ,并分别设置title属性为 "用户名" 和 " 密码" ,然后拖放两个Text Field ,再拖放一个UIButton title属性设置为登录,布局随意,

为两个TextField 设置Placeholder属性,这个就是水印属性,用来添加水印提示,当点击输入任何一个字符的时候就会消失。。

为控件连线,分别为两个TextField 连接连个插座变量 分别命名为textUserName 和 textPwd ,为UIButton 连接一个action 事件,命名为btnClick。(注意红色箭头标注的关键点)

    

为连个TextField 添加键盘消失的方法,首先让 wsqViewController 遵循 UITextFieldDelegate 协议 ,

@interface wsqViewController : UIViewController<UITextFieldDelegate>

并且在 wsqViewController.m 中重写- (BOOL)textFieldShouldReturn:(UITextField *)textField;方法,

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

其实主要是让两个TextField 在结束的时候调用 resignFirstResponder 方法,通过将代理 delegate 指向File's Owner,在单击小键盘return 键和敲键盘回车键的时候让小键盘消失。

因为我们是在单击UIbutton的时候来判断是否登录成功,所以,所有的赋值和取值的操作都可以放在btnClick 事件里面来操作,

但是这样会在单击button的时候增加响应时间,所以我们把一些基本的操作还是放在- (void)viewDidLoad 里面,这里面主要就是

把数据库拷贝直应用程序的沙盒中,因为在mainBundle 目录下,所有系统的文件都是不可以通过程序来操作的,任何操作都会返回nil。

操作其他应用程序也不行,这样每一个应用程序都在自己的沙盒中运行,从而不会影响其他的应用程序。

在 wsqViewController.h 添加一个实例变量,为NSString 类型,用来存放数据库在沙盒中的路径。不要忘记在 dealloc 中release,并且在.m文件中实现它:@synthesize tempPath;

在viewDidLoad 中添加代码:主要是获取user.db在目录中的路径然后通过 NSFileManager 类中的方法,将数据库拷贝至沙盒目录下。

- (void)viewDidLoad
{
[super viewDidLoad];
//获取 user.db在目录中的路径
NSString* strBundlePath = [[NSBundle mainBundle] pathForResource:@"user.db" ofType:nil];
//获取应用程序沙盒下所有文件夹 的路径
NSArray* arrayDocunmentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取应用程序沙盒下 doucumnet文件夹 的路径
NSString* strDocunmentPath = [arrayDocunmentPath objectAtIndex:];
//为文件夹路径附加文件路径,
NSString* destinationPath = [strDocunmentPath stringByAppendingString:@"user.db"];
NSFileManager* fManager = [NSFileManager defaultManager];
//实例化一个NSFileManager 将文件拷贝至沙盒目录下。
if(![fManager fileExistsAtPath:destinationPath]){
NSError* error= nil;
[fManager copyItemAtPath:strBundlePath toPath:destinationPath error:&error];
if(error){
NSLog(@"%@",error);
}
}
//retain 一次,不然会在出了括号之后就会释放,
tempPath = [destinationPath retain];
}

在wsqViewController.h 文件中包含第三方库的头文件:

#import "FMDatabase.h"

在btnClick 方法中添加代码:

- (IBAction)btnClick:(id)sender {
[textUserName resignFirstResponder];
[textPwd resignFirstResponder]; //创建一个第三方库的对象 ,用数据库路径来初始化,
FMDatabase* fmDatebase = [[FMDatabase alloc] initWithPath:tempPath];
//连接数据库,并做判断
if(![fmDatebase open]){
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"数据库连接" message:@"连接失败" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
//显示消息框,
[alert show];
// alloc 出来的空间必须释放
[alert release];
}
//sql语句
NSString* executeStr = [NSString stringWithFormat:@"select * from \"member\" where username =\"%@\" and password = \"%@\"",textUserName.text,textPwd.text];
//sql语句执行之后返回一个FMResultSet容器。
FMResultSet * fmSet = [fmDatebase executeQuery:executeStr];
//扑捉sql异常错误,
if([fmDatebase hadError]){
NSLog(@"%i ,%@",[fmDatebase lastErrorCode],[fmDatebase lastErrorMessage]);
return ;
}
//循环容器
while ([fmSet next]) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"success" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
[alert release];
}
if([fmDatebase hadError]){
NSLog(@"%i ,%@",[fmDatebase lastErrorCode],[fmDatebase lastErrorMessage]);
return ;
} //关闭连接,以及容器
[fmDatebase close];
[fmSet close];
//alloc 出来的空间必须释放。
[fmDatebase release]; }

然后点击run 运行结果如下:

最后为应用程序添加事先准备的图标和应用程序启动动画:

lanch.jpg 和icon.jpg

打开 .plist文件 ,修改icon 。并且添加 一项lanch

修改后结果如下图:

#import <UIKit/UIKit.h>
#import "FMDatabase.h" @interface wsqViewController : UIViewController<UITextFieldDelegate> {
UITextField *textUserName;
UITextField *textPwd;
NSString* tempPath;
} @property (retain, nonatomic) IBOutlet UITextField *textUserName;
@property (retain, nonatomic) IBOutlet UITextField *textPwd;
@property (retain,nonatomic) NSString* tempPath;
- (IBAction)btnClick:(id)sender; @end
//
// wsqViewController.m
// loginTest
//
// Created by administrator on 13-9-4.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
// #import "wsqViewController.h" @implementation wsqViewController
@synthesize textUserName;
@synthesize textPwd;
@synthesize tempPath; - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
} #pragma mark - View lifecycle - (void)viewDidLoad
{
[super viewDidLoad];
//获取 user.db在目录中的路径
NSString* strBundlePath = [[NSBundle mainBundle] pathForResource:@"user.db" ofType:nil];
//获取应用程序沙盒下所有文件夹 的路径
NSArray* arrayDocunmentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取应用程序沙盒下 doucumnet文件夹 的路径
NSString* strDocunmentPath = [arrayDocunmentPath objectAtIndex:];
//为文件夹路径附加文件路径,
NSString* destinationPath = [strDocunmentPath stringByAppendingString:@"user.db"];
NSFileManager* fManager = [NSFileManager defaultManager];
//实例化一个NSFileManager 将文件拷贝至沙盒目录下。
if(![fManager fileExistsAtPath:destinationPath]){
NSError* error= nil;
[fManager copyItemAtPath:strBundlePath toPath:destinationPath error:&error];
if(error){
NSLog(@"%@",error);
}
} tempPath = [destinationPath retain]; } - (void)viewDidUnload
{
[self setTextUserName:nil];
[self setTextPwd:nil];
[super viewDidUnload]; } - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
} - (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
} - (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[textUserName resignFirstResponder];
[textPwd resignFirstResponder];
return YES;
} - (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
} - (void)dealloc {
[textUserName release];
[textPwd release];
[tempPath release];
[super dealloc];
}
- (IBAction)btnClick:(id)sender {
[textUserName resignFirstResponder];
[textPwd resignFirstResponder]; //创建一个第三方库的对象 ,用数据库路径来初始化,
FMDatabase* fmDatebase = [[FMDatabase alloc] initWithPath:tempPath];
//连接数据库,并做判断
if(![fmDatebase open]){
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"数据库连接" message:@"连接失败" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
//显示消息框,
[alert show];
// alloc 出来的空间必须释放
[alert release];
}
//sql语句
NSString* executeStr = [NSString stringWithFormat:@"select * from \"member\" where username =\"%@\" and password = \"%@\"",textUserName.text,textPwd.text];
//sql语句执行之后返回一个FMResultSet容器。
FMResultSet * fmSet = [fmDatebase executeQuery:executeStr];
//扑捉sql异常错误,
if([fmDatebase hadError]){
NSLog(@"%i ,%@",[fmDatebase lastErrorCode],[fmDatebase lastErrorMessage]);
return ;
}
//循环容器
while ([fmSet next]) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"success" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
[alert release];
}
if([fmDatebase hadError]){
NSLog(@"%i ,%@",[fmDatebase lastErrorCode],[fmDatebase lastErrorMessage]);
return ;
} //关闭连接,以及容器
[fmDatebase close];
[fmSet close];
//alloc 出来的空间必须释放。
[fmDatebase release]; }
@end

Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录的更多相关文章

  1. iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法

    iOS程序集成的第三方库过多时,很容易出现某几个库同时用到了一样的函数库,也就是在你的程序link时会提示duplicate symbol,而重复的符号又不是由你自己程序的代码造成的,也就说没法通过直 ...

  2. Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)

    使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...

  3. Objective-C ,ios,iphone开发基础:几个常用类-NSNumber

    2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...

  4. [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  5. Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  6. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

  7. Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

    json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...

  8. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  9. Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器

    第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...

随机推荐

  1. .NET Reactor 命令行使用

    安装.NET Reactor工具软件.例如你的安装目录为:D:\Program Files\Eziriz\.NET Reactor 按如下步骤设置系统环境变量path. 将path变量的值中加入.NE ...

  2. HDU4456-Crowd(坐标旋转+二位树状数组+离散化)

    转自:http://blog.csdn.net/sdj222555/article/details/10828607 大意就是给出一个矩阵 初始每个位置上的值都为0 然后有两种操作 一种是更改某个位置 ...

  3. 关于scrollTop的那些事

    大家在实际项目中,应该是要经常用到scrollTop的,它表示的是可视窗口距离页面顶部的距离,这个scrollTop是可读写的,所以可以用来做页面滚动. 但是大家或多或少遇到一些浏览器兼容问题,为什么 ...

  4. Hibernate之基于主键映射的一对一关联关系

    1. 基于主键的映射策略:指一端的主键生成器使用foreign策略,表明根据"对方"的主键来生成自己的主键,自己并不能独立生成主键.并用<param> 子元素指定使用当 ...

  5. CodeForces 489D Unbearable Controversy of Being (搜索)

    Unbearable Controversy of Being 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/B Descrip ...

  6. #c word转换PDF

    需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...

  7. Session,Cookie 和local storage的区别

    以前从没有听说过local storage, 在网上查了一些资料,得到如下结论 从存储位置看,分为服务器端存储和客户端存储两种 服务器端: session 浏览器端: cookie, localSto ...

  8. [置顶] 新修改ADB,支持Android 4.2 系统 ,全部中文命令,手机屏幕截图等等

    发过好几个ADB的工具,有很多朋友用了之后给我反馈了不少的意见和bug,这里非常感谢他们,所以今天花了一天的时间重新整理了一下ADB,并且修改了这些BUG.也有朋友建议我给一个修改列表,今天发这个帖子 ...

  9. [转]Oracle关于null的处理

    转至:http://www.2cto.com/database/201209/157606.html 关于空值null的排序问题   Oracle排序中NULL值处理的五种常用方法:    1.缺省O ...

  10. 山东理工大学ACM平台题答案关于C语言 1580 闰年

    闰年 Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又成熟了一些 ...