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. 插入 ...
随机推荐
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...
- 【ShaderToy】开篇
写在前面 呜呼,好久没有写博客了,好惭愧.题外话,感觉越大就越想家,希望可以一直和家人在一起,哪怕只是坐在一起不说话也觉得很温暖,一想到要分开眼睛就开始酸,哎.开学还是爬上来老实更新博客学习吧~ 今天 ...
- (一〇〇)使用AddressBookUI实现通讯录操作
上节提到使用AddressBook可以实现通讯录数据的获取,但有时需要用户自己选取联系人或者联系人信息,这时候就要借助AddressBookUI框架的ABPeoplePickerNavigationC ...
- ASP.net 路径问题 详解
各位有没有碰到在日常工作中经常在路径设置的时候把 "~/ ../ .../ . / .http://www.cnblogs.com/"这些符号搞混搞乱了?偶尔还会因路径的问题郁闷了 ...
- Android开发学习之路--网络编程之xml、json
一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...
- (七十五)CoreLocation(一)在iOS7和iOS8设备上获取授权
苹果在iOS8上更新了CoreLocation的授权获取方式,在原来的基础上,不仅需要调用授权函数,还需要对info.plist进行相应的配置. 在iOS上获取经纬度使用的是CoreLocationM ...
- JSP标签JSTL(2)--流程控制
对于流程控制,单纯的用jsp脚本,显得很是繁琐,尤其是遇到if判断的时候,写代码的时候就需要特别的小心,因为极有可能会出现符号不匹配的状况.但是利用标签语言就会大大的改善这一状况. 流程控制 if标签 ...
- μC/OS-II与RT-Thread对比——任务调度
在任务调度器的实现上,μC/OS-II和RT-Thread都采用了位图调度(bitmap scheduling),任务优先级的值越小则代表具有越高的优先级,主要区别在于实现形式,是采用多 ...
- mongoDB常见的查询索引(三)
1. _id索引 _id索引是绝大多数集合默认建立的索引 对于每个插入的数据,MongoDB会自动生成一条唯一的_id字段. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- 开源视频监控系统:iSpy
iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...