-------------------------------------基本操作-------------------------------------

#import "ViewController.h"

#import "FMDB.h"

@interface ViewController ()

@property (nonatomic,strong)FMDatabase *dataBase;

@end

@implementation ViewController

- (IBAction)insertData:(id)sender {

//3.增加 数据 (100条 数据随机)

for (int i = 0; i <100; i++) {

NSString *strName = [NSString stringWithFormat:@"8mingyeuxin-%d",i];

NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO t_student (name ,score)VALUES('%@',%.02f)",strName,arc4random_uniform(1000)/10.0];

//执行 //非查询语句  执行的方法

BOOL success =  [self.dataBase executeUpdate:sqlStr];

if (success) {

NSLog(@"添加成功!");

}else{

NSLog(@"添加失败!");

}

}

}

- (IBAction)selectData:(id)sender {

NSString *strSql =  @"SELECT * FROM t_student WHERE score > 60.0 ORDER BY score DESC;";

//查询语句 执行的方法

FMResultSet *set =  [self.dataBase executeQuery:strSql];

while ([set next]) {

//name

//NSString *name = [set stringForColumnIndex:1];

NSString *name = [set stringForColumn:@"name"];

//score

CGFloat score = [set doubleForColumn:@"score"];

NSLog(@"name = %@  score = %f",name,score);

}

}

- (void)viewDidLoad {

[super viewDidLoad];

//1.创建数据库

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student"];

FMDatabase *dataBase = [FMDatabase databaseWithPath:path];

self.dataBase = dataBase;

BOOL success = [dataBase open];

if (success) {

NSLog(@"数据库创建成功!");

//2.创建表

NSString *str = @"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL NOT NULL)";

if ([self.dataBase executeUpdate:str]) {

NSLog(@"表创建成功!");

}else{

NSLog(@"创建表失败!");

}

}else{

NSLog(@"数据库创建失败!");

}

}

@end

-------------------------------------原子操作-------------------------------------

#import "ViewController.h"

#import "FMDB.h"

@interface ViewController ()

@property (nonatomic,strong)FMDatabaseQueue *dataBaseQ;

@end

@implementation ViewController

//线程安全 公共资源 A使用 的时候 B不能使用

//int a = 110;

//

//100 - 90;

//A  a = 10;

//

//排队等待:

//10 + 100

//

//B  a = 110;

- (IBAction)insertData:(id)sender {

[self.dataBaseQ inDatabase:^(FMDatabase *db) {

//3.增加 数据 (100条 数据随机)

for (int i = 0; i <100; i++) {

NSString *strName = [NSString stringWithFormat:@"8mingyeuxin-%d",i];

NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO t_student (name ,score)VALUES('%@',%.02f)",strName,arc4random_uniform(1000)/10.0];

//执行 //非查询语句  执行的方法

BOOL success =  [db executeUpdate:sqlStr];

if (success) {

NSLog(@"添加成功!");

}else{

NSLog(@"添加失败!");

}

}

}];

}

- (IBAction)selectData:(id)sender {

[self.dataBaseQ inDatabase:^(FMDatabase *db) {

NSString *strSql =  @"SELECT * FROM t_student WHERE score > 60.0 ORDER BY score DESC;";

//查询语句  执行的方法

FMResultSet *set =  [db executeQuery:strSql];

while ([set next]) {

//name

//NSString *name = [set stringForColumnIndex:1];

NSString *name = [set stringForColumn:@"name"];

//score

CGFloat score = [set doubleForColumn:@"score"];

NSLog(@"name = %@  score = %f",name,score);

}

}];

}

- (void)viewDidLoad {

[super viewDidLoad];

//打开数据库 如果没有就创建

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"data.sqlite"];

//创建数据库的队列

FMDatabaseQueue *dataBaseQ = [FMDatabaseQueue databaseQueueWithPath:path];

self.dataBaseQ = dataBaseQ;

[dataBaseQ inDatabase:^(FMDatabase *db) {

BOOL success = [db open];

if (success) {

NSLog(@"数据库创建成功!");

//2.创建表

NSString *str = @"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL NOT NULL)";

if ([db executeUpdate:str]) {

NSLog(@"表创建成功!");

}else{

NSLog(@"创建表失败!");

}

}else{

NSLog(@"数据库创建失败!");

}

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

FMDB 排它锁的更多相关文章

  1. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  2. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

  3. 【原】FMDB源码阅读(一)

    [原]FMDB源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 说实话,之前的SDWebImage和AFNetworking这两个组件我还是使用过的,但是对于 ...

  4. IOS FMDB 获取数据库表和表中的数据

    ios开发中,经常会用到数据库sqlite的知识,除了增,删,改,查之外,我们说说如何获取数据库中有多少表和表相关的内容. 前言 跟数据库使用相关的一般的增删改查的语句,这里就不做解释了.在网上有很多 ...

  5. 简单的数据库设计及使用(FMDB)

    有这样一个需求: 有m个用户公用n个文件,一个用户可能会用到多个文件,一个文件可能被多个用户使用: 如果某个用户离开,那这个用户就不再使用任何文件:如果某个文件没有任何用户使用,就要删除该文件: 已知 ...

  6. FMDB的使用方法

    转自:http://blog.devtang.com/blog/2012/04/22/use-fmdb/ 前言 SQLite (http://www.sqlite.org/docs.html) 是一个 ...

  7. [ios]关于用FMDB 操作数据库 删除 tableView 后刷新

    刚了解使用fmdb,从数据库获取数据 绑定到一个可变数组classNameItems //从ClassList表取得数据 FMResultSet *classInfo=[db executeQuery ...

  8. IOS数据存储之FMDB数据库

    前言: 最近几天一直在折腾数据库存储,之前文章(http://www.cnblogs.com/whoislcj/p/5485959.html)介绍了Sqlite 数据库,SQLite是一种小型的轻量级 ...

  9. FMDB第三方框架

    FMDB是同AFN,SDWebImage同样好用的第三方框架,它以OC的方式封装了SQLite的C语言API,使得开发变得简单方便. 附上github链接https://github.com/ccgu ...

随机推荐

  1. 【先定一个小目标】windows下安装RabbitMQ消息服务器

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. 1:安装RabbitMQ 需要先安装Erlang语言开发包.下载地址  ...

  2. 了解了下spring boot,说一下看法

    这段时间比较忙,新项目的事比较多,跟着老大忙前忙后,没准备写博客. 下班地铁上看视频,发现spring boot的公开课,看完后,就准备抒抒情怀: 1.从个人的角度来看,使用spring boot可能 ...

  3. 查询EBS中LOV的SQL语句

    1.帮助->关于:查找会话 SID : 507: 2.点一下LOV右边的三点,触发LOV事件: 3.运行如下代码段: DECLARE  l_sid NUMBER := :SID;BEGIN  F ...

  4. Linux下编译安装MariaDB

    MariaDB是MySQL的一个开源分支,主要是社区在维护,并且完全兼容MySQL,并且可以很方便的称为MySQL的替代,MariaDB的诞生正是出自MySQL创始人Michael Widenius之 ...

  5. The difference between QA, QC, and Test Engineering

    Tuesday, March 06, 2007 Posted by Allen Hutchison, Engineering Manager and Jay Han, Software Enginee ...

  6. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  7. Android 通用流行框架

    原文出处: http://android.jobbole.com/83028/ 1. 缓存 名称 描述 DiskLruCache Java实现基于LRU的磁盘缓存 2.图片加载 名称 描述 Andro ...

  8. C++小结

    1.输入:cin>>变量名: 输出:cout<<变量名<<endl: 2.类 public 公有,此类及其他类中使用   private 私有,只能在本类中使用   ...

  9. Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器

    public static void DownFileStream(MemoryStream ms, string fileName) { if (ms !=Stream.Null) { ) { fi ...

  10. 小米4 miui专用 Xposed安装器86版

    转载自 http://www.52pojie.cn/thread-516435-1-1.html 写在前面:各位用xp受到不同限制,有些机型还找不到框架包,又要刷第三方rec又要谨慎选择框架版本.官方 ...