p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822d0f }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 }
p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 }
p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 }
p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421; min-height: 21.0px }
span.s1 { }
span.s2 { font: 18.0px "PingFang SC" }
span.s3 { color: #822d0f }
span.s4 { font: 18.0px Menlo }
span.s5 { color: #c81b13 }
span.s6 { color: #c42275 }
span.s7 { color: #000000 }
span.s8 { color: #3c828b }
span.s9 { color: #6122ae }
span.s10 { color: #294c50 }
span.s11 { color: #539aa4 }
span.s12 { color: #3e1e81 }
span.s13 { font: 18.0px "PingFang SC"; color: #c81b13 }
span.s14 { color: #1e9421 }
span.s15 { font: 18.0px "PingFang SC"; color: #1e9421 }
span.s16 { color: #0435ff }
span.s17 { color: #703daa }
//
// ViewController.m
// 04 - FMDB线程安全的用法
//
// Created by 李洪强 on 2017/6/6.
// Copyright ? 2017年 李洪强. All rights reserved.
//
#import "ViewController.h"
//导入头文件
#import "FMDB.h"
@interface ViewController ()
@property(nonatomic,strong)FMDatabaseQueue *dataBaseQ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"student"];
FMDatabaseQueue *dataBaseQ = [FMDatabaseQueue databaseQueueWithPath:path];
_dataBaseQ = dataBaseQ;
[_dataBaseQ inDatabase:^(FMDatabase * _Nonnull db) {
BOOL success = [db open];
if(success){
NSLog(@"创建数据库成功");
//创建表
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(@"创建数据库失败");
}
}];
}
//增加数据
- (IBAction)insertData:(UIButton *)sender {
[_dataBaseQ inDatabase:^(FMDatabase * _Nonnull db) {
for(int i = 0; i < 100 ; i++){
NSString *strName = [NSString stringWithFormat:@"ming-yuexing-%d",i];
NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO t_student (name , score) VALUES (‘%@‘,%.2f)",strName,arc4random_uniform(1000)/10.0];
BOOL success = [db executeUpdate:sqlStr];
if(success){
NSLog(@"添加成功");
}else{
NSLog(@"添加失败");
}
}
}];
}
- (IBAction)selectData:(UIButton *)sender {
[_dataBaseQ inDatabase:^(FMDatabase * _Nonnull db) {
NSString *sqlStr = @"SELECT * FROM t_student WHERE score > 60.0 ORDER BY score DESC";
//执行查询语句
FMResultSet *set = [db executeQuery:sqlStr];
while ([set next]) {
NSString *name = [set stringForColumn:@"name"];
CGFloat score = [set doubleForColumn:@"score"];
NSLog(@"name = %@ score = %f",name,score);
}
}];
}
/*
使用FMDB比sql的好处:
线程安全
公共资源在A使用的时候,B不能修改
*/
@end