李洪強iOS開發之FMDB線程安全的用法

//

//? 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);

? ? ? ? }

? ? }];

?? ?

}

?

/*

?使用FMDBsql的好處:

?線程安全

?公共資源在A使用的時候,B不能修改

?

?

?

?

?

?

?*/

?

@end

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541267.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541267.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541267.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SCHAR_MIN常數,C ++中的示例

C SCHAR_MIN宏常量 (C SCHAR_MIN macro constant) SCHAR_MIN constant is a macro constant which is defied in climits header, it is used to get the minimum value of a signed char object, it returns the minimum value that a signed char object can store, which i…

android分開兩個線程做事,android開發教程之handle實現多線程和異步處理

這次淺談一下Handler,為什么會出現Handler這個功能特性呢&#xff1f;首先&#xff0c;在之前的基本控件&#xff0c;基本都是在Activity的onCreate(Bundle savedInstanceState)方法中調用和處理的&#xff0c;但是&#xff0c;在有些情況&#xff0c;比如在網絡上下載軟件等一…

夏夜

兒時的夏夜畢竟是最有夏夜的味道。屋堂的煤油燈啪嗒的跳動&#xff0c;忽明忽暗&#xff0c;真怕它脆弱的明亮突然變黑暗。屋外彎月星稀&#xff0c;月光優雅的撒在平靜的湖面上&#xff0c;清純而又溫和。水鳥在湖岸邊慵懶的伸了伸脖子。正享受著夏夜的寧靜和清涼。調皮的小孩…

Python operator.lt()函數與示例

operator.lt()函數 (operator.lt() Function) operator.lt() function is a library function of operator module, it is used to perform "less than operation" on two values and returns True if the first value is less than the second value, False, otherw…

android實現滑動切換圖,Android:使用ViewPager實現左右滑動切換圖片加點點

圖片發自簡書App1、引入android-support-v4.jar包&#xff0c;在主布局里加入< ?xml version"1.0" encoding"utf-8"?>< RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.…

div的替代品

人們在標簽使用中最常見到的錯誤之一就是隨意將HTML5的<section>等價于<div>——具體地說&#xff0c;就是直接用作替代品(用于樣式)。在XHTML或者HTML4中&#xff0c;我們常看到這樣的代碼&#xff1a; <!-- HTML 4-style code --> <div id"wrapper…

threadgroup_Java ThreadGroup list()方法與示例

threadgroupThreadGroup類的list()方法 (ThreadGroup Class list() method) list() method is available in java.lang package. list()方法在java.lang包中可用。 list() method is used to list or display information (like name, priority, thread group, etc.) to the st…

html的柱狀圖去除右邊縱坐標,excel如何把次坐標軸逆序

excel2010設置水平坐標軸逆序類型,但垂直坐標軸刻設置水平坐標軸逆序且垂直坐標軸軸仍位于圖表左側的步驟是&#xff1a;打開帶有圖表的Excel工作表&#xff1b;在圖表區域的水平軸標簽位置雙擊鼠標左鍵&#xff0c;打開“設置坐標軸格式”&#xff1b;勾尋逆序類別”&#xff…

jhipster項目遷移websocket

2019獨角獸企業重金招聘Python工程師標準>>> 1、 在項目目錄下命令行安裝 兩個組件 bower install sockjs-client bower install stomp-websocket 2、在index.html 中加入組件js的引用 <script src"bower_components/sockjs-client/dist/sockjs.js">…

Java包hashCode()方法及示例

包類hashCode()方法 (Package Class hashCode() method) hashCode() method is available in java.lang package. hashCode()方法在java.lang包中可用。 hashCode() method is used to return the hashcode of the package calculated from the package name. hashCode()方法用…

html 圖片上放置按鈕,用CSS在圖片上再加一個小按鈕

很簡單的&#xff0c;嗯&#xff0c;就是要做成這樣的&#xff1a;用CSS&#xff0c;當然得用層了&#xff0c;就是在圖片上再加一個層&#xff0c;用來放那個按鈕&#xff0c;按鈕又有兩種方式可以放&#xff0c;一種是直接用圖片img標簽&#xff0c;一種是通過背景圖片放上去…

html點擊導航變色,點擊導航后,當前導航的顏色變色

html>Documentnav ul {padding: 0;list-style: none;}nav ul li {text-decoration: none;width: 70px;line-height: 50px;text-align: center;float: left;background-color: #000;color: #fff;cursor: pointer;}.gray {background-color: rgba(0,0,0,0.6);}首頁實戰路徑猿問…

Java掃描儀toString()方法及示例

掃描儀類toString()方法 (Scanner Class toString() method) toString() method is available in java.util package. toString()方法在java.util包中可用。 toString() method is used to string denotation of this Scanner and it contains information related to tracing.…

怎么將oracle的sql文件轉換成mysql的sql文件

怎么將sql文件導入PowerDesigner中的方法(將oracle的sql文件轉換成mysql的sql文件)呢&#xff1f; 怎么將xx.sql文件的數據庫結構導入powerdesigner 的方法呢? 現講下。 工具/原料 PowerDesigner軟件&#xff0c;oracle數據庫 方法/步驟 將要導入的庫的所有表的表結構(不要表數…

ios html圖片相對路徑,iOS 下加載本地HTML/js/css/image 等路徑問題

今天在項目中遇到一個問題&#xff1a;我將H5的文件拖入項目中&#xff0c;在webView上添加H5,運行時發現H5的樣式與圖片等都沒屏幕快照 2016-07-06 11.33.22.png有了。經過多種測試后發現&#xff1a;是路徑的問題。在ios項目下添加本地HTML/js/css/image 當拖入項目時有兩種選…

nextfloat_Java Random nextFloat()方法與示例

nextfloat隨機類nextFloat()方法 (Random Class nextFloat() method) nextFloat() method is available in java.util package. nextFloat()方法在java.util包中可用。 nextFloat() method is used to generate the next pseudo-random float value between the range 0.0 and …

mac自己定義tree命令

編輯文件: vim ~/.bash_profile 在文件末尾追加: alias tree"find . -print | sed -e s;[^/]*/;|____;g;s;____|; |;g" 應用配置: source ~/.bash_profile 轉載于:https://www.cnblogs.com/gavanwanggw/p/6962533.html

Java Hashtable keySet()方法與示例

哈希表類keySet()方法 (Hashtable Class keySet() method) keySet() method is available in java.util package. keySet()方法在java.util包中可用。 keySet() method is used to return a set of keys to be viewed in a set in this Hashtable. keySet()方法用于返回要在此H…

el-calendar 怎么設置上一年和下一年_為什么香港碩士一年的含金量那么高?

為什么香港碩士研究生只讀一年但含金量更高&#xff1f;近些年來香港留學大熱&#xff0c;其中一個原因就是香港的授課式碩士研究生僅需一年就可以拿到學位。對于學生來說&#xff0c;在享受到國際一流的教學資源和工作機會的同時&#xff0c;既節省時間又節約金錢&#xff0c;…

武漢市江岸區2021年高考成績查詢,2021年武漢各區一、二、三批次高中有哪些(名單)...

5月5日至9日是武漢市中考網上報名填報志愿的日子&#xff0c;填報志愿之前應提前了解本區的一批次、二批次和三批次學校的名單&#xff0c;了解其歷年錄取分數線&#xff0c;并選中最合適最理想的一所學校進行填寫&#xff0c;下面我們來看詳細名單。武漢各區一、二、三批次高中…