iOS中 用FMDB封装一个SQLite数据库
建立一个单例:
DataBaseHandle.h
#import <Foundation/Foundation.h> @class PersonModel; @class FMDatabase; @interface DataBaseHandle : NSObject @property(nonatomic,retain)FMDatabase *db; //创建单例的的接口 + (DataBaseHandle *)shareDateBaseHandle; //创建一个Person表格 - (void)creatPersonTable; //插入person的方法 - (void)insertPersonTable : (PersonModel *)person; //写一个删除人的接口 - (void)deletePersonByPerssonID : (NSString *)ID; //写一个修改人的接口 - (void)uodatePerson : (NSString *)age ByPersonID : (NSString *)ID; //写一个查询所有人的接口 - (NSMutableArray *)selectAllPersonFromPersonTable; @end
DataBaseHandle.m
#import "DataBaseHandle.h"
#import "FMDB.h"
#import "PersonModel.h"
@implementation DataBaseHandle
- (void)dealloc
{
self.db = nil;
[super dealloc];
}
创建单例的的接口:
//创建单例对象使其存在于静态区
static DataBaseHandle *handle = nil;
//创建单例的的借口
+ (DataBaseHandle *)shareDateBaseHandle{
@synchronized(self){
if (handle == nil) {
handle = [[DataBaseHandle alloc]init];
}
}
return handle;
}
写一个私有的方法,返回数据库的路径
- (NSString *)dbpath{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"db.sqlite"];
}
创建一个Person表格
//创建一个Person表格
- (void)creatPersonTable{
//初始化数据库对象
self.db = [FMDatabase databaseWithPath: [self dbpath]];
//打开数据库
BOOL isOpen = [self.db open];
if (isOpen) {
NSLog(@"打开成功");
//创建表
BOOL isCreat = [self.db executeUpdate:@"create table if not exists Person(id integer primary key autoincrement,name text,gender text,age integer,salary integer)"];
NSLog(@"%@",isCreat ? @"创建成功":@"创建失败");
}else{
NSLog(@"打开失败");
}
}
四种方法:增、删、改、查;
//插入person的方法
- (void)insertPersonTable : (PersonModel *)person{
BOOL isInsert = [self.db executeUpdate:@"insert into Person(name,age)values(?,?)",person.name,person.age];
NSLog(@"%@",isInsert ? @"插入成功":@"插入失败");
}
//写一个删除的接口
- (void)deletePersonByPerssonID : (NSString *)ID{
BOOL isDelete = [self.db executeUpdate:@"delete from Person where id = ?",ID];
NSLog(@"%@",isDelete ? @"删除成功":@"删除失败");
}
//写一个修改人的接口
- (void)uodatePerson : (NSString *)age ByPersonID : (NSString *)ID{
BOOL isUpdate = [self.db executeUpdate:@"update Person set age = ? where id = ?",age,ID];
NSLog(@"%@",isUpdate ? @"修改成功":@"修改失败");
}
//写一个查询所有人的接口
- (NSMutableArray *)selectAllPersonFromPersonTable{
FMResultSet *set = [self.db executeQuery:@"select * from Person"];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
while ([set next]) {
NSInteger ID = [set intForColumn:@"id"];
NSString *name = [set stringForColumn:@"name"];
NSInteger age = [set intForColumn:@"age"];
//创建Person对象存储信息
PersonModel *p = [[PersonModel alloc]init];
p.ID = [NSString stringWithFormat:@"%ld",ID];
p.name = name;
p.age = [NSString stringWithFormat:@"%ld",age];
//添加到数组
[array addObject:p];
[p release];
}
return array;
}
建一个model类
PersonModel.h
#import <Foundation/Foundation.h>
@interface PersonModel : NSObject
@property(nonatomic,copy)NSString *ID;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *age;
@end
PersonModel.m
#import "PersonModel.h"
@implementation PersonModel
- (void)dealloc
{
self.name = nil;
self.age = nil;
self.ID = nil;
[super dealloc];
}
@end
===============================测试调用===============================
#import "FirstViewController.h"
#import "DataBaseHandle.h"
#import "PersonModel.h"
@interface FirstViewController ()
@property(nonatomic,retain)NSMutableArray *dataSource;//接收查询的结果
@end
@implementation FirstViewController
- (void)dealloc
{
self.dataSource = nil;
[super dealloc];
}
//懒加载
- (NSMutableArray *)dataSource{
if (_dataSource == nil) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return [[_dataSource retain]autorelease];
}
TEXT:
- (void)viewDidLoad {
[super viewDidLoad];
//调用并验证
[[DataBaseHandle shareDateBaseHandle]creatPersonTable];
NSLog(@"%@",NSHomeDirectory());
PersonModel *p = [[PersonModel alloc]init];
p.name = @"小韩哥";
p.age = @"20";
//调用插入person的方法
// [[DataBaseHandle shareDateBaseHandle]insertPersonTable:p];
//接收数据库返回的查询结果
self.dataSource = [[DataBaseHandle shareDateBaseHandle]selectAllPersonFromPersonTable];
//调用删除人的方法
[[DataBaseHandle shareDateBaseHandle]deletePersonByPerssonID:@"9"];
}
配置显示:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstcell" forIndexPath:indexPath];
PersonModel *p = self.dataSource[indexPath.row];
cell.textLabel.text = p.name;
return cell;
}
布局预览:
提示:重在封装SQLite思想,不在效果,能有效调用即可!
iOS中 用FMDB封装一个SQLite数据库的更多相关文章
- 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密
在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...
- iOS中使用FMDB事务批量更新数据库
今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了"事务"这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕 ...
- IOS开发-UI学习-sqlite数据库的操作
IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...
- 在VB中利用Nuget包使用SQLite数据库和Linq to SQLite
上午解决了在C#中利用Nuget包使用SQLite数据库和Linq to SQLite,但是最后生成的是C#的cs类文件,对于我这熟悉VB而对C#白痴的来说怎么能行呢? 于是下午接着研究,既然生成的是 ...
- 创建一个 SQLite 数据库
首先,我们学习如何创建一个SQLite 数据库.如果想要在data/example.sqlite 这个路径中创建一个示例数据库,就必须确保该路径存在.如果该路径不存在,就必须先创建路径:if (!di ...
- 将 flask 中的 session 存储到 SQLite 数据库中
将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...
- Electron中使用sql.js操作SQLite数据库
推荐sql.js——一款纯js的sqlite工具. 一.关于sql.js sql.js(https://github.com/kripken/sql.js)通过使用Emscripten编译SQLite ...
- android 一个SQLite数据库多个数据表的基本使用框架 (带demo)
android 一个SQLite数据库多个数据表(带demo) 前言 demo演示 一.搭建 二.建立实体类 三.建立数据库操作类 ...
- 在ios中使用FMDB
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...
随机推荐
- 安装redis 执行make命令时报错解决方法
一.未安装GCC 解决方法:执行yum install gcc-c++命令安装GCC,完成后再次执行make命令 yum install gcc-c++ Linux无法连接网络 http://www. ...
- sublime下配置C/C++运行环境
最近在学习<WEB前端课程>老师教我们使用DW,但是不太喜欢,就选择了sublime,写前端代码还是很方便. 平时都是写C++,C比较多,借鉴了别人的配置步骤,将sublime打造成IDE ...
- 验证码识别之w3cschool字符图片验证码(easy级别)
起因: 最近在练习解析验证码,看到了这个网站的验证码比较简单,于是就拿来解析一下攒攒经验值,并无任何冒犯之意... 验证码所在网页: https://www.w3cschool.cn/checkmph ...
- jQuery 学习笔记一
- 作业03-面向对象入门.md
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你 ...
- 无法启动postgresql的错误
chown postgres /etc/ssl/private/ssl-cert-snakeoil.key chgrp postgres /etc/ssl/private/ssl-cert-snake ...
- Android仅2步实现 滚粗 汉堡导航栏效果~ 全新底部导航交互(滑动隐藏)
本文同步自wing的地方酒馆 布吉岛大家有木有看这一篇文章,再见,汉堡菜单,我们有了新的 Android 交互设计方案 本库下载地址:https://github.com/githubwing/Bye ...
- 复杂和遗留的数据库schema
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50414652 In this chapter, we focus ...
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- 搭建ejabberd集群
搭建ejabberd集群(金庆的专栏 2016.8)以2台机器搭建一个ejabberd集群.2台机器都是外网一块网卡,内网另一块网卡.新建一个域名,添加2台机器的外网IP.分别用源码安装ejabber ...