• FMDB 排它锁


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

    #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

  • 相关阅读:
    关于findViewById返回空指针的错误
    android客户端向服务器发送图片和文字,类似于发微博。能用json格式发送吗?
    nodejs 学习资料大全
    篇章三:[AngularJS] 使用AngularCSS動態載入CSS
    篇章二:[AngularJS] 使用AngularAMD動態載入Service
    篇章一:[AngularJS] 使用AngularAMD動態載入Controller
    Angular 资料大集合
    js-音乐播放器,播放|暂停|滑块的功能
    JS-以鼠标位置为中心的滑轮放大功能demo1
    使用 Electron 构建桌面应用(拖动控制篇)
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4722246.html
Copyright © 2020-2023  润新知