//

//  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. javascript之事件处理

    一般事件 onclick                       鼠标点击时触发此事件 ondblclick                  鼠标双击时触发此事件 onmousedown    ...

  2. javascript之事件模型

    事件模型 冒泡型事件(Bubbling):事件由叶子节点沿祖先节点一直向上传递到根节点 捕获型事件(Capturing):由DOM树最顶元素一直到最精确的元素,与冒泡型事件相反 DOM标准事件模型:D ...

  3. Dynamics CRM2013/2015 Plugin注册工具Register New Assembly时无法看到注册按钮的解决办法

    CRM2013的注册插件工具UI相比2011之前有了一定的改变,但改变UI的同时也给开发人员带来了困扰,打开注册工具点击Register按钮选择dll时页面就是下面这样的,你完全看不到最下面的两个按钮 ...

  4. INV 调试: 如何获取库存物料事务处理调试信息

     1. 按如下方式设置系统配置文件值: 系统配置文件值 地点/用户/应用/职责层配置文件值 --汇总 FND: 启用调试日志   是 FND:调试日志层级   陈述 INV: 调试跟踪: 是 IN ...

  5. path和classpath的区别

    path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中设置JDK的路径也可执行JAVA文件,但必须把完整的路径写出来,如C:\Program Files\Java\jdk1.6 ...

  6. 我眼中的Linux设备树(三 属性)

    三 属性(property)device_type = "memory"就是一个属性,等号前边是属性,后边是值.节点是一个逻辑上相对独立的实体,属性是用来描述节点特性的,根据需要一 ...

  7. protobuf代码生成

    windows : 1,两个文件:proto.exe, protobuf-java-2.4.1.jar 2,建立一个工程TestPb,在下面建立一个proto文件件,用来存放[.proto]文件 3, ...

  8. XML解析之JAXP案例详解

    根据一个CRUD的案例,对JAXP解析xml技术,进行详细的解释: 首先,已知一个xml文件中的数据如下: <?xml version="1.0" encoding=&quo ...

  9. 看uboot的时候发现随机数的另外一种算法

    #include <stdio.h> #include <time.h> static unsigned int y = 1U; unsigned int rand_r(uns ...

  10. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...