SQLite資料庫架構--FMDB簡單介紹

來源:互聯網
上載者:User

標籤:

1.什麼是FMDB

FMDB是iOS平台的SQLite資料庫架構

FMDB以OC的方式封裝了SQLite的C語言API

 

2.FMDB的優點

使用起來更加物件導向,省去了很多麻煩、冗餘的C語言代碼

對比蘋果內建的Core Data架構,更加輕量級和靈活

提供了多安全執行緒的資料庫操作方法,有效地防止資料混亂

 

3.FMDB的github地址

https://github.com/ccgus/fmdb

 

二、核心類

FMDB有三個主要的類

(1)FMDatabase

一個FMDatabase對象就代表一個單獨的SQLite資料庫

用來執行SQL語句

 

(2)FMResultSet

使用FMDatabase執行查詢後的結果集

 

(3)FMDatabaseQueue

用於在多線程中執行多個查詢或更新,它是安全執行緒的

 

三、開啟資料庫

通過指定SQLite資料庫檔案路徑來建立FMDatabase對象

FMDatabase *db = [FMDatabase databaseWithPath:path];

if (![db open]) {

    NSLog(@"資料庫開啟失敗!");

}

 

檔案路徑有三種情況

(1)具體檔案路徑

  如果不存在會自動建立

 

(2)Null 字元串@""

  會在臨時目錄建立一個空的資料庫

  當FMDatabase串連關閉時,資料庫檔案也被刪除

 

(3)nil

  會建立一個記憶體中臨時資料庫,當FMDatabase串連關閉時,資料庫會被銷毀

 

四、執行更新

在FMDB中,除查詢以外的所有操作,都稱為“更新”

create、drop、insert、update、delete等

 

使用executeUpdate:方法執行更新

- (BOOL)executeUpdate:(NSString*)sql, ...

- (BOOL)executeUpdateWithFormat:(NSString*)format, ...

- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments

 

樣本

[db executeUpdate:@"UPDATE t_student SET age = ? WHERE name = ?;", @20, @"Jack"]

 

五、執行查詢

查詢方法

- (FMResultSet *)executeQuery:(NSString*)sql, ...

- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...

- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments

 

樣本

// 查詢資料

FMResultSet *rs = [db executeQuery:@"SELECT * FROM t_student"];

 

// 遍曆結果集

while ([rs next]) {

    NSString *name = [rs stringForColumn:@"name"];

    int age = [rs intForColumn:@"age"];

    double score = [rs doubleForColumn:@"score"];

}

 

六、程式碼範例

1.建立一個項目,匯入libsqlite3庫,並在項目中包含主標頭檔

2.下載第三方架構FMDB

  

3.範例程式碼

  YYViewController.m檔案

 1 // 2 //  YYViewController.m 3 //  04-FMDB基本使用 4 // 5 //  Created by apple on 14-7-27. 6 //  Copyright (c) 2014年 wendingding. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "FMDB.h"11 12 @interface YYViewController ()13 @property(nonatomic,strong)FMDatabase *db;14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     //1.獲得資料庫檔案的路徑22     NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];23     NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"];24     25     //2.獲得資料庫26     FMDatabase *db=[FMDatabase databaseWithPath:fileName];27     28     //3.開啟資料庫29     if ([db open]) {30         //4.創表31         BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];32         if (result) {33             NSLog(@"創表成功");34         }else35         {36             NSLog(@"創表失敗");37         }38     }39     self.db=db;40 41 }42 43 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event44 {45     [self delete];46     [self insert];47     [self query];48 }49 50 //插入資料51 -(void)insert52 {53     for (int i = 0; i<10; i++) {54         NSString *name = [NSString stringWithFormat:@"jack-%d", arc4random_uniform(100)];55         // executeUpdate : 不確定的參數用?來佔位56         [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", name, @(arc4random_uniform(40))];57         //        [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);" withArgumentsInArray:@[name, @(arc4random_uniform(40))]];58         59         // executeUpdateWithFormat : 不確定的參數用%@、%d等來佔位60         //        [self.db executeUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];61     }62 }63 64 //刪除資料65 -(void)delete66 {67     //    [self.db executeUpdate:@"DELETE FROM t_student;"];68     [self.db executeUpdate:@"DROP TABLE IF EXISTS t_student;"];69     [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];70 }71 72 //查詢73 - (void)query74 {75     // 1.執行查詢語句76     FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_student"];77     78     // 2.遍曆結果79     while ([resultSet next]) {80         int ID = [resultSet intForColumn:@"id"];81         NSString *name = [resultSet stringForColumn:@"name"];82         int age = [resultSet intForColumn:@"age"];83         NSLog(@"%d %@ %d", ID, name, age);84     }85 }86 87 @end

列印查看結果:

提示:

如果ID設定為逐漸,且設定為自動成長的話,那麼把表中的資料刪除後,重新插入新的資料,ID的編號不是從0開始,而是接著之前的ID進行編號。

注意:

  不要寫成下面的形式,不要加‘‘,直接使用%@,它會自動認為這是一個字串。

 

SQLite資料庫架構--FMDB簡單介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.