//

//  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

IOS使用FMDB封装的数据库增删改查操作的更多相关文章

  1. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  2. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

  3. Android_SQLite数据库增删改查操作

    一:什么是SQLite? 在Android平台上,集成了一个嵌入式关系型轻量级的数据库. 二:什么时候用的数据库? 有大量相似机构的数据需要存储时. 三:如何创建一个数据库? 1.创建一个Sqlite ...

  4. jmeter-Java-MongoDB 数据库增删改查操作

    在日常测试过程中会发现有些测试数据是通过数据库来获取的,一般常用的数据比如SQL .Oracle,此类数据库jmeter有专门的插件进行使用JDBC,今天跟大家说一说关于Mongodb这个数据库jme ...

  5. SQLite数据库增删改查操作

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串 ...

  6. Android SQLite数据库增删改查操作

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...

  7. java连接mysql数据库增删改查操作记录

    1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...

  8. SpringBoot结合Mybatis 使用 mapper*.xml 进行数据库增删改查操作

    什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyBa ...

  9. 数据库-增删改查操作SQL实现

    一.数据插入-Insert 1. 插入单条记录 insert into 表名(字段名,字段名,字段名) //当插入所有字段时,字段名可以省略 values('值1','值2','值3'); 2. 插入 ...

随机推荐

  1. NET中小型企业级项目开发架构系列(一)

    前端时间我们开发了基于Net的一套搭建sprint.NET+NHibernate+MVC+WCF+EasyUI等中小型企业级系统开发平台,现在把整个开发过程中的步步进展整理出来和大家分享,这个系列可能 ...

  2. 查看linux的进程到底用了多少内存

    1. 在linux下,查看一个运行中的程序, 占用了多少内存, 一般的命令有    (1). ps aux:      其中  VSZ(或VSS)列 表示,程序占用了多少虚拟内存.          ...

  3. Android Studio设置代理更新下载SDK

    代理主机和端口号按下图设置即可,便可以轻松的下载更新SDK啦~~~

  4. UNIX网络编程——带外数据小结

    TCP没有真正的带外数据,不过提供紧急模式和紧急指针.一旦发送端进入紧急模式,紧急指针就出现在发送到对端的分节中的TCP首部中.连接的对端收取该指针是在告知接收进程发送端已经进入紧急模式,而且该指针指 ...

  5. linux命令指usermod(管理用户以及权限的命令)

    usermod命令:用来修改用户帐号的各项设定. 示例:usermod -a -G usergroupnewuser 或者usermod -aGusergroup newuser 语法:usermod ...

  6. 【Unity技巧】自定义消息框(弹出框)

    写在前面 这一篇我个人认为还是很常用的,一开始也是实习的时候学到的,所以我觉得实习真的是一个快速学习工程技巧的途径. 提醒:这篇教程比较复杂,如果你不熟悉NGUI.iTween.C#的回调函数机制,那 ...

  7. Uva - 12174 - Shuffle

    用滑动窗口的思想,用一个数组保存每个数在窗口中出现的次数.再用一个变量记录在窗口中恰好出现一次的的数的个数,这样可以枚举所有可能的答案,判断它所对应的所有串口,当且仅当所有的串口均满足要求时这个答案可 ...

  8. Linux 共享内存 详解

    一.什么是共享内存区 共享内存区是最快的可用IPC形式.它允许多个不相关的进程去访问同一部分逻辑内存.如果需要在两个运行中的进程之间传输数据,共享内存将是一种效率极高的解决方案.一旦这样的内存区映射到 ...

  9. Scipy教程 - 距离计算库scipy.spatial.distance

    http://blog.csdn.net/pipisorry/article/details/48814183 在scipy.spatial中最重要的模块应该就是距离计算模块distance了. fr ...

  10. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...