标签:使用 创建数据库 ase rem value font 失败 好处 sql
//
// 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
标签:使用 创建数据库 ase rem value font 失败 好处 sql
原文地址:http://www.cnblogs.com/LiLihongqiang/p/6952539.html