IOS開發——使用資料庫

來源:互聯網
上載者:User

標籤:

IOS開發——使用FMDB資料庫簡介需求作用:

如果需要儲存大量的結構較為複雜的資料的時候,使用資料庫,例如交規考試項目

1、資料庫的基本介紹

資料庫(DB)是一種資料模型組織起來並存放儲存管理的資料倉儲。它是由檔案管理發展起來的,如今的資料庫基本上都是關係型資料庫。

資料庫的基本操作是增、刪、查、改。

常見的幾種資料庫,Oracle,Access,SQL Server,DB2,Mysql

手機上使用的資料庫為SQLite,因為它佔用資源很好,易於配置等原因。

2、mesaSQLite 使用

執行個體: 使用資料存放區儲存一個班上學生的資訊

學號:sid 使用者名稱:username 密碼:password 成績:score 1501 zhangs 123 100 1502 heihei 321 90

(1)建立資料庫

可以直接在功能表列中選中new database 建立出一個新的資料庫。

(2)建立資料表

資料表的建立方式比較多一點,可以在structure中點擊+號進行建立。然

在SQL Query中輸入建立語句 create table  tablename(id 類型,name 類型) (直接在下方的輸入框中輸入)

 

(3)SQL結構化查詢語句

首先必須要有資料,加入資料的操作為insert  into tablename (id,name) values (11,‘zhang‘)

  1.  尋找所有 :select * from tablename
  2. 尋找單個屬性:select name from tablename
  3. 尋找多個屬性 :select id ,name from tablename
  4. 按一定條件尋找:select id from tablename where id =XX
  5. 按多個條件尋找:select id from tablename where id =XX and name = XX
  6.  條件中含有特定字串的尋找:select id from tablename where name like ‘%XX%‘

(4)簡單的where條件

  1. = 等於
  2. > 大於
  3. < 小於
  4. >= 大於等於
  5. <= 小於等於
  6. <> 不等於
  7. !> 不大於
  8. %匹配的字串% 匹配字串
3、在IOS開發中使用FMDB

FMDB 是一個開源的第三方架構。

FMDatabase -表示一個單獨的資料庫,用來執行SQLite的命令。

FMResultSet -表示FMDatabase執行之後,返回的結果集。

FMDatabaseQuere-是在多線程的時候,使用這個類。

(1)匯入FMDB

直接載入到項目中,並加上sqlite3.dylib的二進位庫(尋找sqlite)。

在需要的檔案中,加上標頭檔 #import "FMDatabase.h"

 

 

(2)建立並開啟資料庫

 

 NSString *path = [[NSBundle mainBundle ] pathForResource:@"data.sqlite" ofType:nil];    _database = [[FMDatabase alloc] initWithPath:path];    if (!_database.open) {        NSLog(@"開啟失敗");    }else{         NSLog(@"成功");    }

(3)查詢操作,並得到返回集

  NSString *sql = @"select * from firstlevel";               FMResultSet *resultset = [_database executeQuery:sql];        NSMutableArray *mArray = [[NSMutableArray alloc] init];        while ([resultset next]) {       Model *model = [[Model alloc] init];       model.id = [resultset stringForColumn:@"id"];       model.name = [resultset stringForColumn:@"name"];        [mArray addObject:model];}

 

 

結語:天天加油。。。。。。

 

IOS開發——使用資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.