IOS使用FMDB封装的数据库增删改查操作
//
// 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封装的数据库增删改查操作的更多相关文章
- (转)SQLite数据库增删改查操作
原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- Android_SQLite数据库增删改查操作
一:什么是SQLite? 在Android平台上,集成了一个嵌入式关系型轻量级的数据库. 二:什么时候用的数据库? 有大量相似机构的数据需要存储时. 三:如何创建一个数据库? 1.创建一个Sqlite ...
- jmeter-Java-MongoDB 数据库增删改查操作
在日常测试过程中会发现有些测试数据是通过数据库来获取的,一般常用的数据比如SQL .Oracle,此类数据库jmeter有专门的插件进行使用JDBC,今天跟大家说一说关于Mongodb这个数据库jme ...
- SQLite数据库增删改查操作
一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串 ...
- Android SQLite数据库增删改查操作
一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...
- java连接mysql数据库增删改查操作记录
1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...
- SpringBoot结合Mybatis 使用 mapper*.xml 进行数据库增删改查操作
什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyBa ...
- 数据库-增删改查操作SQL实现
一.数据插入-Insert 1. 插入单条记录 insert into 表名(字段名,字段名,字段名) //当插入所有字段时,字段名可以省略 values('值1','值2','值3'); 2. 插入 ...
随机推荐
- shell 参数列表的获取&shell使用的一些总结
最近在修改公司的一些cron,自己也是第一次接触和学习shell.对于一些零散但是常用的知识点,做一点点的总结. 拿出一个方法说说吧,方法如下:(信息量挺大的,请耐心看下面的说明) trans_cou ...
- Objc运行时读取和写入plist文件遇到的问题
下面是本猫保持游戏NPC和物件交互的plist文件: 随着游戏和玩家逐步发生互动,玩家会修改人物和物件的交互的状态.这也是RPG游戏最基本的功能. 在切换每个地图时需要将上一个地图发生的改变存储到pl ...
- 深入分析Spring混合事务
在ORM框架的事务管理器的事务内,使用JdbcTemplate执行SQL是不会纳入事务管理的. 下面进行源码分析,看为什么必须要在DataSourceTransactionManager的事务内使用J ...
- 详解EBS接口开发之WIP模块接口
总体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对采用并发请求方式和调用API方式分别进行介绍 内容 WIP模块常用标准表简介 WIP事物处理组成 WIP相关业务流程 WIP相关 ...
- Git版本控制:Gitlab及Coding.net的使用
http://blog.csdn.net/pipisorry/article/details/50709014 Gitlab介绍 GitLab是利用 Ruby on Rails 一个开源的版本管理系统 ...
- android下在屏幕适配小总结
为什么要屏幕适配?为此我就不说了,网上处理方法要么让你用几套不同分辨率的图片,要么写几套布局文件,要么就是在xml中写dip(这个还是可以的),前面两种感觉过程工作量太大了,由加载大图片的优化思想 同 ...
- 学生信息管理小系统(以XML为存储方式)
为了更好地应用XML,就写了这个小项目. 下面是我的项目的目录结构 项目思路 dao是Date Access Object 数据访问层,主要是负责操作数据 domain是实体层,类似于bean层,放置 ...
- nginx 平滑升级
怎么能在不停止服务的情况下,平滑的升级nginx?下面告诉你答案,其实很简单 1.下载nginx新版本,并解压,进入解压的目录 2.你要执行旧版本的nginx -V来查看旧版本编译的时候,编译了什么模 ...
- mysql进阶(六)模糊查询的四种用法介绍
mysql中模糊查询的四种用法介绍 这篇文章主要介绍了mysql中模糊查询的四种用法,需要的朋友可以参考下. 下面介绍mysql中模糊查询的四种用法: 1 %: 表示任意0个或多个字符.可匹配任意类型 ...
- studio多渠道打包
由于国内Android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包,如果让你打几十个市场的包岂不烦死了,不过有了Gradle,这事就简单了. 友盟多渠道打包 废话 ...