//
// DBHelper.h
// LessonStoryBoard
//
// Created by 袁冬冬on 15/10/29.
// Copyright (c) 2015年袁冬冬. All rights reserved.
//
#import
#import "FMDB.h"
@interfaceDBHelper : NSObject
@property(nonatomic, strong) FMDatabaseQueue*databaseQueue; //数据库
- (void)openDB:(NSString*)dbName; //打开数据库,并创建数据库对象
- (void)executeupdate:(NSString*)sql; //执行更新SQL语句,用于插入、修改、删除
- (NSArray*)executeQuery:(NSString*)sql; //执行查询语句
@end
//
// DBHelper.m
// LessonStoryBoard
//
// Created by 袁冬冬on 15/10/29.
// Copyright (c) 2015年袁冬冬. All rights reserved.
//
#import "DBHelper.h"
@implementationDBHelper
- (void)openDB:(NSString*)dbName {
//获取数据库路径,通常保存到沙盒中
NSString*filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:dbName];
NSLog(@"%@",filePath);
//创建FMDatabaseQueue对象
self.databaseQueue= [FMDatabaseQueuedatabaseQueueWithPath:filePath];
}
- (void)executeupdate:(NSString*)sql {
//执行更新SQL语句
[self.databaseQueueinDatabase:^(FMDatabase*db) {
[db executeUpdate:sql];
}];
}
- (NSArray*)executeQuery:(NSString*)sql {
NSMutableArray*array = [NSMutableArrayarray];
[self.databaseQueueinDatabase:^(FMDatabase*db) {
//执行查询语句
FMResultSet*result = [db executeQuery:sql];
while(result.next) {
NSMutableDictionary*dic = [NSMutableDictionarydictionary];
for(inti = 0; i < result.columnCount; i++) {
dic[[result columnNameForIndex:i]] = [result stringForColumnIndex:i];
}
[array addObject:dic];
}
}];
returnarray;
}
@end
//注册
//
// RegisterViewController.m
// LessonStoryBoard
//
// Created by 袁冬冬on 15/10/29.
// Copyright (c) 2015年袁冬冬. All rights reserved.
//
#import "RegisterViewController.h"
#import "DBHelper.h"//数据库操作类
@interfaceRegisterViewController ()
@property(weak, nonatomic) IBOutletUITextField *usernameTF; //用户名
@property(weak, nonatomic) IBOutletUITextField *passwordTF; //密码
@property(weak, nonatomic) IBOutletUITextField *rePasswordTF; //确认密码
@property(weak, nonatomic) IBOutletUITextField *emailTF; //邮箱
@property(weak, nonatomic) IBOutletUITextField *phoneTF; //手机号
@end
@implementationRegisterViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)reBackClick:(UIButton*)sender {
[selfsaveDataToDataBase]; //将数据存储到数据库
[self.navigationControllerpopViewControllerAnimated:YES];
}
#pragma mark - save data in database
- (void)saveDataToDataBase {
DBHelper*dbHelper = [[DBHelperalloc] init];
[dbHelper openDB:@"contact.sqlite"]; //打开数据库,创建数据库对象
//创建表
[dbHelper executeupdate:@"create table if not exists t_user(username text primary key,password text,email text,phone text)"];
//插入信息
[dbHelper executeupdate:[NSStringstringWithFormat: @"insert into t_user(username,password,email,phone) values(%@,%@,%@,%@)",self.usernameTF.text,self.passwordTF.text,self.emailTF.text,self.phoneTF.text]];
}
@end
//登陆
//
// LoginViewController.m
// LessonStoryBoard
//
// Created by 袁冬冬on 15/10/29.
// Copyright (c) 2015年袁冬冬. All rights reserved.
//
#import "LoginViewController.h"
#import "ListTableViewController.h"
#import "DBHelper.h"
@interfaceLoginViewController ()
@property(weak, nonatomic) IBOutletUITextField *userNameTF; //用户名文本框
@property(weak, nonatomic) IBOutletUITextField *passwordTF; //密码文本框
//默认的账号密码
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *password;
@end
@implementationLoginViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.name = @"admin";
self.password = @"123456";
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Action
//登录按钮响应事件
- (IBAction)LoginClick:(UIButton *)sender {
//获取数据库中的用户名和密码
NSDictionary *dic = [selfgainDataFromDataBase];
NSString *myname = dic[@"username"];
NSString *mypw = dic[@"password"];
//创建UIAlertController
if([self.userNameTF.text isEqualToString:myname] && [self.passwordTF.text isEqualToString:mypw]) {
//获取下一个视图控制器
ListTableViewController*listVC = [self.storyboardinstantiateViewControllerWithIdentifier:@"list"];
[selfalertController:@"欢迎回来"viewController:listVC];
} else{
[selfalertController:@"账号或密码错误"viewController:nil];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
//alertController提示框
- (void)alertController:(NSString*)message viewController:(UITableViewController*)controller {
UIAlertController*alertVC = [UIAlertControlleralertControllerWithTitle:@"温馨提示"message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction*action = [UIAlertActionactionWithTitle:@"好"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
[self.navigationControllerpushViewController:controller animated:YES];
}];
[alertVC addAction:action];
[selfpresentViewController:alertVC animated:YEScompletion:nil];
}
#pragma mark - data from dataBase
- (NSDictionary*)gainDataFromDataBase {
DBHelper*dbHelper = [[DBHelperalloc] init];
[dbHelper openDB:@"contact.sqlite"]; //打开数据库,创建数据库对象
NSArray*array = [dbHelper executeQuery:[NSStringstringWithFormat:@"select * from t_user where username = %@ and password = %@",self.userNameTF.text,self.passwordTF.text]];
returnarray[0];
}
@end