//

//  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. 1.2、Android Studio为新设备创建一个模块

    模块为你的应用的源码.资源文件和app level设置(比如AndroidManifest.xml)提供了一个容器.每个模块可以独立的构建.测试和调试. 通过使用模块,Android Studio可以 ...

  2. Java基础---Java---IO流-----BufferedReader、BufferedWriter、缓冲区、装饰设计模式及和继承的区别

    IO流 IO流用来处理设备之间的数据传输 java对数据的操作是过流的方式 流按操作数据分为两种:字节流与字符流 流按流向分为:输入流,输出流. IO流常用基类 字节流的抽象基类:InputStrea ...

  3. JDBC编程学习笔记之数据库连接池的实现

    在JDBC编程的时候,获取到一个数据库连接资源是很宝贵的,倘若数据库访问量超大,而数据库连接资源又没能得到及时的释放,就会导致系统的崩溃甚至宕机.造成的损失将会是巨大的.再看有了数据库连接池的JDBC ...

  4. Java中类的创建及类与对象的关系

    //import java.util.Scanner; //创建一个类 class Person{ //属性和方法的定义不是必须的 //属性 String name ; int age ; //方法 ...

  5. MySQL慢查询优化 EXPLAIN详解

            我们平台过一段时间就会把生产数据库的慢查询导出来分析,要嘛修改写法,要嘛新增索引.以下是一些笔记.总结整理 慢查询排查         show status;  // 查询mysql ...

  6. Ubuntu 安装 Mysql 5.6 数据库

    Ubuntu 安装 Mysql 5.6 数据库 1)下载: mysql-5.6.13-debian6.0-x86_64.deb http://dev.mysql.com/downloads/mirro ...

  7. python字符串与数字类型转化

    数字转字符串:str(数字),如str(10) 相反:int(字符串),如int('10') 另外,import string后 用string.atoi('100',base),转换为int,bas ...

  8. javascript之DOM文档对象模型编程的引入

    /* DOM(Document Object Model) 文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建一个对应的 对象进行描述,我们在浏览器上看 ...

  9. Chipmunk Rigid Bodies:cpBody

    Chipmunk刚体支持3种不同的类型: Dynamic(动态),Static(静态)以及Kinematic(混合态)刚体.它们拥有不同的行为和性能特征. 动态刚体是默认的刚体类型.它们可以对碰撞做出 ...

  10. Html5学习之旅-html5的留言记事本开发(17)

    web留言记事本的开发 !!!!!代码如下 index.html的代码 <!DOCTYPE html> <html lang="en"> <head& ...