Li Hongqiang iOS Development fmdb thread-safe usage

Source: Internet
Author: User

//

Viewcontroller.m

04-fmdb Thread-safe usage

//

Created by Li Hongqiang on 2017/6/6.

Copyright? year Li Hongqiang . All rights reserved.

//

#import "ViewController.h"

Import Header File

#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:p ath];

_databaseq = databaseq;

[_databaseq indatabase: ^ (fmdatabase * _nonnull db) {

BOOL success = [db open];

if(success) {

NSLog(@ " CREATE database Success ");

// Create a table

nsstring *str = @ "CREATE TABLE IF not EXISTS t_student (id INTEGER PRIMARY KEY autoincrement, name TEXT not NU Ll,score REAL not NULL) ";

if([db executeupdate: str]) {

NSLog(@ " CREATE TABLE successfully !") );

}Else{

NSLog(@ " failed to create TABLE !") );

}

}Else{

NSLog(@ " failed to create database ");

}

}];

}

Add Data

-(ibaction) InsertData: (UIButton *) Sender {

[_databaseq indatabase: ^ (fmdatabase * _nonnull db) {

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

nsstring *strname = [nsstring stringwithformat:@ "ming-yuexing-%d", I];

nsstring *sqlstr = [nsstring stringwithformat:@ "INSERT into T_student (name, score) VALUES ( '%@ ',%.2f) ", StrName,arc4random_uniform(+)/10.0];

BOOL success = [db executeupdate: sqlstr];

if(success) {

NSLog(@ " Add success ");

}Else{

NSLog(@ " add failed ");

}

}

}];

}

-(ibaction) Selectdata: (UIButton *) Sender {

[_databaseq indatabase: ^ (fmdatabase * _nonnull db) {

nsstring *sqlstr = @ "SELECT * from T_student WHERE score > 60.0 ORDER by Score DESC";

// Execute query statement

fmresultset *set = [db executeQuery: sqlstr];

while ([set next]) {

nsstring *name = [Set stringforcolumn:@ "name"];

cgfloat score = [Set doubleforcolumn:@ "score"];

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

}

}];

}

/*

Benefits of using FMDB over SQL :

Thread Safety

when a public resource is used in a , B cannot be modified .

*/

@end

Li Hongqiang iOS Development fmdb thread-safe usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.