自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

IOS 數(shù)據(jù)存儲之 FMDB 詳解

移動(dòng)開發(fā) iOS
FMDB是面向?qū)ο蟮?,它以O(shè)C的方式封裝了SQLite的C語言API,使用起來更加的方便,不需要過多的關(guān)心數(shù)據(jù)庫操作的知識。但是它本身也存在一些問題,比如跨平臺,因?yàn)樗怯胦c的語言封裝的,所以只能在ios開發(fā)的時(shí)候使用,如果想實(shí)現(xiàn)跨平臺的操作,來降低開發(fā)的成本和維護(hù)的成本,就需要使用比較原始的SQLite。

FMDB是用于進(jìn)行數(shù)據(jù)存儲的第三方的框架,它與SQLite與Core Data相比較,存在很多優(yōu)勢。

FMDB是面向?qū)ο蟮?,它以O(shè)C的方式封裝了SQLite的C語言API,使用起來更加的方便,不需要過多的關(guān)心數(shù)據(jù)庫操作的知識。但是它本身也存在一些問題,比如跨平臺,因?yàn)樗怯胦c的語言封裝的,所以只能在ios開發(fā)的時(shí)候使用,如果想實(shí)現(xiàn)跨平臺的操作,來降低開發(fā)的成本和維護(hù)的成本,就需要使用比較原始的SQLite。

Core Data是ORM的一種體現(xiàn),使用Core Data需要用到模型數(shù)據(jù)的轉(zhuǎn)化,雖然操作簡單,不需要直接操作數(shù)據(jù)庫,但是性能沒有直接使用SQLite高。但是SQLite使用的時(shí)候需要使用c語言中的函數(shù),操作比較麻煩,因此需要對它進(jìn)行封裝。但是如果只是簡單地封裝,很可能會忽略很多重要的細(xì)節(jié),比如如何處理并發(fā)以及安全性更問題。

因此,在這里推薦使用第三方框架FMDB,它是對libsqlite3框架的封裝,用起來的步驟與SQLite使用類似,并且它對于多線程的同時(shí)操作一個(gè)表格時(shí)進(jìn)行了處理,也就意味著它是線程安全的。FMDB是輕量級的框架,使用靈活,它是很多企業(yè)開發(fā)的首選。
FMDB中重要的類

FMDatabase:一個(gè)FMDatabase對象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫,用來執(zhí)行SQL語句

FMResultSet:使用FMDatabase執(zhí)行查詢后的結(jié)果集

FMDatabaseQueue:用于在多線程中執(zhí)行多個(gè)查詢或更新,它是線程安全的

FMDB使用步驟

1. 下載FMDB文件 fmdb下載地址 ,將FMDB文件夾添加到項(xiàng)目中

2. 導(dǎo)入sqlite框架,導(dǎo)入FMDatabase.h文件

3.與SQLite使用步驟類似,需要獲取數(shù)據(jù)庫文件路徑,然后獲得數(shù)據(jù)庫,并打開數(shù)據(jù)庫,然后數(shù)據(jù)庫進(jìn)行操作,最后關(guān)閉數(shù)據(jù)庫。代碼如下所示:
 

  1. // ViewController.m 
  2. // JRFMDB 
  3. // 
  4. // Created by jerehedu on 15/6/18. 
  5. // Copyright (c) 2015年 jerehedu. All rights reserved. 
  6. // 
  7.  
  8. #import "ViewController.h" 
  9. #import "FMDatabase.h" 
  10.  
  11. @interface ViewController () 
  12. @property (nonatomic, strong) FMDatabase *db; 
  13.  
  14. @end 
  15.  
  16. @implementation ViewController 
  17.  
  18. - (void)viewDidLoad 
  19. [super viewDidLoad]; 
  20. //導(dǎo)入sqlite框架,導(dǎo)入FMDB文件夾 
  21.  
  22. //1.獲得數(shù)據(jù)庫文件的路徑 
  23. NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
  24. NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"]; 
  25. NSLog(@"fileName = %@",fileName); 
  26.  
  27. //2.獲得數(shù)據(jù)庫 
  28. FMDatabase *db = [FMDatabase databaseWithPath:fileName]; 
  29.  
  30. //3.打開數(shù)據(jù)庫 
  31. if ([db open]) { 
  32. NSLog(@"ok"); 
  33.  
  34. //4.創(chuàng)表 
  35. BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; 
  36. if (result) { 
  37. NSLog(@"創(chuàng)表成功"); 
  38. }else
  39. NSLog(@"創(chuàng)表失敗"); 
  40. self.db=db; 
  41.  
  42. //插入數(shù)據(jù) 
  43. [self insertStu]; 
  44. [self deleteStu:6]; 
  45. [self updateStu:@"apple7_name" :@"7777"]; 
  46.  
  47. [self queryStu]; 
  48. [self dropStu]; 
  49. [self insertStu]; 
  50. [self queryStu]; 
  51.  
  52. //6.關(guān)閉數(shù)據(jù)庫 
  53. [self.db close]; 
  54.  
  55. #pragma mark 插入數(shù)據(jù) 
  56. -(void)insertStu 
  57. for (int i=0; i<10; i++) 
  58. NSString *name = [NSString stringWithFormat:@"1apple%i_name",i]; 
  59. int age = arc4random()%3+20
  60.  
  61. //1. executeUpdate : 不確定的參數(shù)用?來占位 (后面參數(shù)必須都是oc對象) 
  62. [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)]; 
  63.  
  64. //2. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來占位 (參數(shù)為原始數(shù)據(jù)類型) 
  65. // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age]; 
  66.  
  67. //3. 數(shù)組 
  68. // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]]; 
  69.  
  70.  
  71. #pragma mark 刪除數(shù)據(jù) 
  72. -(void)deleteStu:(int)idNum 
  73. //a. executeUpdate : 不確定的參數(shù)用?來占位 (后面參數(shù)必須都是oc對象) 
  74. // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)]; 
  75.  
  76. //b. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來占位 
  77. // [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"]; 
  78.  
  79. #pragma mark 銷毀表格 
  80. -(void)dropStu 
  81. [self.db executeUpdate:@"drop table if exists t_student;"]; 
  82.  
  83. //4.創(chuàng)表 
  84. BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; 
  85. if (result) { 
  86. NSLog(@"再次創(chuàng)表成功"); 
  87. }else
  88. NSLog(@"再次創(chuàng)表失敗"); 
  89.  
  90. #pragma mark 修改數(shù)據(jù) 
  91. -(void)updateStu:(NSString *)oldName :(NSString*)newName 
  92. // [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName]; 
  93. [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName]; 
  94.  
  95. #pragma mark 查詢數(shù)據(jù) 
  96. -(void)queryStu 
  97. //1.執(zhí)行查詢語句 
  98. // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"]; 
  99. FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)]; 
  100.  
  101. //2.遍歷結(jié)果集合 
  102. while ([resultSet next]) { 
  103. int idNum = [resultSet intForColumn:@"id"]; 
  104. NSString *name = [resultSet objectForColumnName:@"name"]; 
  105. int age = [resultSet intForColumn:@"age"]; 
  106. NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age); 
  107.  
  108.  
  109. - (void)didReceiveMemoryWarning 
  110. [super didReceiveMemoryWarning]; 
  111. // Dispose of any resources that can be recreated. 
  112.  
  113. @end
責(zé)任編輯:chenqingxiang 來源: 博客園
相關(guān)推薦

2014-07-23 08:55:42

iOSFMDB

2013-07-29 11:19:16

iOS開發(fā)iOS開發(fā)學(xué)習(xí)FMDB更新二進(jìn)制圖片

2011-08-02 11:07:42

iOS開發(fā) UIWebView

2018-01-31 08:44:20

數(shù)據(jù)存儲存儲設(shè)備存儲系統(tǒng)

2020-03-17 09:21:20

MariaDBSpider存儲

2011-08-15 17:20:25

iPhone應(yīng)用Sqlite3FMDB

2012-03-01 20:42:12

iPhone

2011-05-31 17:32:32

Android SharedPref

2018-04-27 09:03:57

Redis數(shù)據(jù)存儲

2011-08-05 16:50:00

iPhone 數(shù)據(jù) Sqlite

2013-04-02 10:16:34

iOS學(xué)習(xí)UINavigatio頁面切換

2013-04-02 10:15:45

iOS學(xué)習(xí)UINavigatio添加UIBarButt

2011-08-03 13:28:08

Oracle數(shù)據(jù)庫數(shù)據(jù)庫控制文件

2017-12-06 09:00:14

2011-08-02 11:17:13

iOS開發(fā) View

2013-04-02 10:36:43

iOS學(xué)習(xí)UINavigatioToolBar

2011-03-08 09:58:21

海量數(shù)據(jù)

2018-03-20 09:36:57

數(shù)據(jù)倉庫數(shù)據(jù)存儲知識

2018-06-07 16:33:31

大數(shù)據(jù)冷熱數(shù)據(jù)存儲平臺

2022-04-27 08:17:07

OCMock單元測試集成
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號