//
// DBHelper.h
// LessonStoryBoard
//
// Created by 袁冬冬 on 15/10/29.
// Copyright (c) 2015年 袁冬冬. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FMDB.h"
@interface DBHelper : 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"
@implementation DBHelper
- (void)openDB:(NSString *)dbName {
//获取数据库路径,通常保存到沙盒中
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:dbName];
NSLog(@"%@",filePath);
//创建FMDatabaseQueue对象
self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:filePath];
}
- (void)executeupdate:(NSString *)sql {
//执行更新SQL语句
[self.databaseQueue inDatabase:^(FMDatabase *db) {
[db executeUpdate:sql];
}];
}
- (NSArray *)executeQuery:(NSString *)sql {
NSMutableArray *array = [NSMutableArray array];
[self.databaseQueue inDatabase:^(FMDatabase *db) {
//执行查询语句
FMResultSet *result = [db executeQuery:sql];
while (result.next) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (int i = 0; i < result.columnCount; i++) {
dic[[result columnNameForIndex:i]] = [result stringForColumnIndex:i];
}
[array addObject:dic];
}
}];
return array;
}
@end
//注册
//
// RegisterViewController.m
// LessonStoryBoard
//
// Created by 袁冬冬 on 15/10/29.
// Copyright (c) 2015年 袁冬冬. All rights reserved.
//
#import "RegisterViewController.h"
#import "DBHelper.h" //数据库操作类
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameTF; //用户名
@property (weak, nonatomic) IBOutlet UITextField *passwordTF; //密码
@property (weak, nonatomic) IBOutlet UITextField *rePasswordTF; //确认密码
@property (weak, nonatomic) IBOutlet UITextField *emailTF; //邮箱
@property (weak, nonatomic) IBOutlet UITextField *phoneTF; //手机号
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 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 {
[self saveDataToDataBase]; //将数据存储到数据库
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - save data in database
- (void)saveDataToDataBase {
DBHelper *dbHelper = [[DBHelper alloc] 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:[NSString stringWithFormat: @"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"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF; //用户名文本框
@property (weak, nonatomic) IBOutlet UITextField *passwordTF; //密码文本框
//默认的账号密码
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *password;
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.name = @"admin";
self.password = @"123456";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Action
//登录按钮响应事件
- (IBAction)LoginClick:(UIButton *)sender {
//获取数据库中的用户名和密码
NSDictionary *dic = [self gainDataFromDataBase];
NSString *myname = dic[@"username"];
NSString *mypw = dic[@"password"];
//创建UIAlertController
if ([self.userNameTF.text isEqualToString:myname] && [self.passwordTF.text isEqualToString:mypw]) {
//获取下一个视图控制器
ListTableViewController *listVC = [self.storyboard instantiateViewControllerWithIdentifier:@"list"];
[self alertController:@"欢迎回来" viewController:listVC];
} else {
[self alertController:@"账号或密码错误" 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 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.navigationController pushViewController:controller animated:YES];
}];
[alertVC addAction:action];
[self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark - data from dataBase
- (NSDictionary *)gainDataFromDataBase {
DBHelper *dbHelper = [[DBHelper alloc] init];
[dbHelper openDB:@"contact.sqlite"]; //打开数据库,创建数据库对象
NSArray *array = [dbHelper executeQuery:[NSString stringWithFormat:@"select * from t_user where username = %@ and password = %@",self.userNameTF.text,self.passwordTF.text]];
return array[0];
}
@end