数据存储之第三方FMDB
上周四、周五在忙公司的事情和炒股,没来得及更新博客,这周就补一下,学习总结下FMDB。
FMDB是对sqlite的封装,特别是在多线程情况下,使用sqlite是非常麻烦,而使用FMDB相对简单,下面是使用FMDatabase和FMDatabasequeue的代码例子
//
// ViewController.m
// FMDBDemo
//
// Created by cyw on 15-4-26.
// Copyright (c) 2015年 cyw. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// FMDB常用三个类:
// 1.FMDatabase :单一的sqlite数据库,用于执行sql语句
// 2.FMDatabaseQueue:多线程下执行sql语句
// 3.FMResultSet:sql语句的结果集
// 1.数据库文件路径
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath=[path stringByAppendingPathComponent:@"cyw.sqlite"];
NSLog(@"%@",filePath);
// 创建FMDB对象
// 参数有3中情况:
// 1.文件路径 文件不存在自动创建
// 2.nil 会创建内存中的临时数据库,当FMDatabase关闭时,自动销毁
// 3.@"" 会在临时目录创建空数据库,关闭时销毁
// FMDatabase *db=[[FMDatabase alloc]initWithPath:filePath];
// FMDatabase *bd=[FMDatabase databaseWithPath:filePath];
//打开数据库
// if ([db open]) {
// //新建数据库
// [self createTable:db];
//
// //插入数据
// [self insertData:db];
// //查询数据
// [self selectData:db];
// [db close];
// }
// 数据库有锁和事务,在FMDB中可以用FMDatabaseQueue来实现
// 锁主要解决多线程的问题 事务算是加了一个隐式锁
//创建数据库队列
FMDatabaseQueue *queue=[FMDatabaseQueue databaseQueueWithPath:filePath];
// [self queueSelect:queue];
// [self queueTransaction:queue];
[self queueIntranscation:queue];
}
-(BOOL)createTable:(FMDatabase*)db
{
//此两个sql一起执行报错
//create table if not exists P_Class (CId integer primary key ,CName varchar(20));
NSString *strsql=@"create table if not exists Person(id integer primary key autoincrement ,age integer not null,name text not null,photo blob,registerTime DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')),sex char(1) default '0',money numeric(10,2),classId integer,constraint fk_Person_class foreign key (classId) references P_Class (CId));";
// FMDatabase中更新操作都是使用executeUpdate方法 查询使用executeQuery方法
BOOL result= [db executeUpdate:strsql];
if (result) {
NSLog(@"数据库创建成功");
}
else
{
NSLog(@"数据库创建失败");
}
return result;
}
-(void)insertData:(FMDatabase*)db
{
[db executeUpdate:@"delete from person"];
//executeUpdate方法的不同参数
// [db executeUpdate:@"insert into P_Class(CId,CName) values(?,?);" withArgumentsInArray:@[@1001,@"软件工程"]];
//注意加@
[db executeUpdate:@"insert into person(name,age,money,classId) values(?,?,?,?);",@"cuiyw",@24,@200.3,@1001];
[db executeUpdateWithFormat:@"insert into Person(name,age,money,classId) values(%@,%d,%f,%d)",@"cyw",23,210.9,1001];
}
-(void)selectData:(FMDatabase*)db
{
// 查询数据
NSString *strsql=@"select id,name,age,money from Person";
// FMResultSet返回结果集
FMResultSet *set=[db executeQuery:strsql];
while ([set next]) {
int pid=[set intForColumn:@"id"];
NSString *name=[set stringForColumnIndex:1];
int age=[set intForColumn:@"age"];
float money=[set doubleForColumnIndex:3];
NSLog(@"%d %@ %d %f",pid,name,age,money);
}
NSString *strsql1=@"select CId,CName from P_Class";
// FMResultSet返回结果集
FMResultSet *set1=[db executeQuery:strsql1];
while ([set1 next]) {
NSString *name=[set1 stringForColumnIndex:1];
int age=[set1 intForColumn:@"CId"];
// float money=[set doubleForColumnIndex:2];
NSLog(@"%@ %d ",name,age);
}
}
-(void)queueSelect:(FMDatabaseQueue*)queue
{
//打开数据库
[queue inDatabase:^(FMDatabase *db) {
[self selectData:db];
}];
}
//事务的两种写法
-(void)queueTransaction:(FMDatabaseQueue*)queue
{
[queue inDatabase:^(FMDatabase *db) {
[self selectData:db];
[db beginTransaction];
[db executeUpdate:@"update Person set money=money+20.0 where id=4"];
[db executeUpdate:@"update Person set money=money-20.0 where id=3"];
[db commit];
[self selectData:db];
}];
}
-(void)queueIntranscation:(FMDatabaseQueue*)queue
{
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[self selectData:db];
[db executeUpdate:@"update Person set money=money+20.0 where id=4"];
[db executeUpdate:@"update Person set money=money-20.0 where id=3"];
[self selectData:db];
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
数据存储之第三方FMDB的更多相关文章
- 数据存储之第三方FMDB优化
最近项目要用到数据库,采用的是第三方FMDB, 之前做C#时用过sqlHelper,自己就按着sqlHelper的思路封装了一下,封装的也比较简单,看到网上有一些根据FMDB封装的ORM框架,但基本都 ...
- IOS 数据存储之 FMDB 详解
FMDB是用于进行数据存储的第三方的框架,它与SQLite与Core Data相比较,存在很多优势. FMDB是面向对象的,它以OC的方式封装了SQLite的C语言API,使用起来更加的方便,不需要过 ...
- IOS数据存储之FMDB数据库
前言: 最近几天一直在折腾数据库存储,之前文章(http://www.cnblogs.com/whoislcj/p/5485959.html)介绍了Sqlite 数据库,SQLite是一种小型的轻量级 ...
- 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等
(1)效果 (2)源码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,能够直接用代码的push和pop ...
- 在AndroidStudio中数据存储第三方数据管理Bmob的使用
---恢复内容开始--- 在日常写代码的过程中我们比较痛苦的就是数据库的建立和使用,那么今天来介绍一下一个第三方的数据管理平台Bmonb. 一.我们首先进入Bmob的官网创建一个账号 Bome官网网址 ...
- IOS数据存储之Sqlite数据库
前言: 之前学习了数据存储的NSUserDefaults,归档和解档,沙盒文件存储,但是对于数据量比较大,需要频繁查询,删除,更新等操作的时候无论从效率上还是性能上,上述三种明显不能满足我们的日常开发 ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
- ios中常见数据存储方式以及SQLite常用的语句
在iOS中,根据不同的需求对应的有多种数据存储方式: 1.NSUserdefaults 将数据存储到沙盒中(library),方便易用,但是只能存储系统提供的数据类型(plist),不能存储自定义的 ...
- iOS常用的几种数据存储方式
之前由于刚入行不久,对数据持久化不是很了解,尤其是用数据库存储大量数据的操作.经过摸索就在此总结一下,方便以后查阅 下面就简单介绍一下: 1.NSUserDefaults 感觉最常用的小量数据,属性, ...
随机推荐
- Android学习之可滑动当前的Activity视图看见上一个活动的视图
先看一下我的效果图吧: 好大的图啊!!! 百度音乐由一个很酷的功能,当前的UI可以滑动,然后看见上一个活动的UI,当时顿时觉得百度的牛人好多啊,能将如此前沿的技术应用到app上.当然如果你熟悉了And ...
- How to give a math lecture
摘自 http://www.timhsu.net/courses/generic/proof.pdf 或 http://www.timhsu.net/courses/generic/how-to- ...
- 由异常掉电问题---谈xfs文件系统
由异常掉电问题---谈xfs文件系统 本文皆是作者自己的学习总结或感悟(linux环境),如有不对,欢迎提出一起探讨!! 目录结构 一.相关知识 二.问题提出 三.处理方法 四.最终结果 一.相关知识 ...
- 安装Chrome浏览器
Ubuntu 16.04下安装64位谷歌Chrome浏览器 在 Ubuntu 16.04 中,要想使用谷歌的 Chrome 浏览器,可以通过命令行的方式手动安装. 1.进入 Ubuntu 16.04 ...
- 关于质能等效的两个思想实验 Two Ideological Experiments on Mass-Energy Equivalence
大家知道,物质和能量是等效的,虽然质能方程已暗示了这种等效关系,但并非显而易见.此等效性可以从以下两个思想实验中获知. 实验一:一台电子称上放置一个金属物体,加热它,称的读数将会略微增加.这是因为金属 ...
- asp.net core sdk & runtime 镜像[已更新至2.2.0]
在官方镜像的脚本上, 增加了System.Drawing相关的依赖库 以北京时间为默认的时间 2.2.0 Windows SDK地址: 官方: https://dotnetcli.blob.core. ...
- 迁移桌面程序到MS Store(7)——APPX + Service
本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...
- 【转】selenium webdriver三种等待方法
原文:https://www.cnblogs.com/lgh344902118/p/6015593.html webdriver三种等待方法 1.使用WebDriverWait from seleni ...
- ViewPager无限滑动
2016-6-19 前言 View轮播效果在app中很常见,一想到左右滑动的效果就很容易想到使用ViewPager来实现.对于像我们常说的banner这样的效果,具备无限滑动的功能是可以用ViewPa ...
- nginx-1.服务器是什么
服务器相信很多电脑爱好者都听过或者了解一些,一般我们很难看到真正的服务器,因为服务器一般均放置在机房重点,闲人一般均是免进的.比如我们每天浏览的网站.玩的游戏等,所有的数据均存在服务器,服务器一般都在 ...