iOS 資料庫第三方FMDB的簡單使用

來源:互聯網
上載者:User

標籤:

一 FMDB的簡單說明及介紹

FMDB的github地址 https://github.com/ccgus/fmdb

FMDB是一款簡潔的,易用的封裝庫,簡單介紹一下FMDB的使用

在FMDB下載檔案後,工程中必須匯入如下檔案,並使用libsqlite3.0.tbd依賴包

FMDB同時相容ARC和非ARC工程,會自動根據工程配置來調整相關的記憶體管理代碼.

二.FMDB的核心類

FMDB有三個主要的類

(1)FMDatabase

     一個FMDatabase對象就代表一個單獨的SQLite資料庫,用來執行SQL語句

 

(2)FMResultSet

    使用FMDatabase執行查詢後的結果集(即所查詢結果的集合)

 

(3)FMDatabaseQueue

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

 

三.開啟資料庫

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

path 為所建立資料庫的路徑

FMDatabase *db = [FMDatabase databaseWithPath:path];

if (![db open]){

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

}

檔案路徑有三種情況

(1)具體檔案路徑

如果不存在會自動建立

(2)Null 字元串@""

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

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

(3)nil

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

 

四 執行更新

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

create、drop、insert、update、delete等

 

使用executeUpdate:方法執行更新

樣本

[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

 

範例程式碼

//

//  ViewController.m

//  資料庫

 

//  Copyright © 2016年 zm. All rights reserved.

//

 

#import "ViewController.h"

#import "FMDB.h"

@interface ViewController ()

@property (nonatomic,strong)FMDatabase *db;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

  //1. 擷取資料庫檔案的路徑

    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    

    NSString *fileName= [doc stringByAppendingPathComponent:@"student.sqlite"];

    

    //2.擷取資料庫

    FMDatabase *db = [FMDatabase databaseWithPath:fileName];

    

    //3.開啟資料庫

    if ([db open]) {

        //創表

        BOOL result = [db executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement,name text not null,age integer not null);"];

        if (result) {

            NSLog(@"建立表成功");

        }else{

            

            NSLog(@"建立表失敗");

        }

    }

    

    self.db = db;

 

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    

    [self delete];

    [self insert];

    [self query];

    

    

}

//刪除資料

 

- (void)delete{

    

    [self.db executeUpdate:@"drop table if exests t_student;"];

    [self.db executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement,name text not null, age integer not null);"];

    

    

    

}

 

//插入資料

- (void)insert{

    

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

        NSString *name = [NSString stringWithFormat:@"jack-%d",arc4random_uniform(40)];

        

        [self.db executeUpdate:@"insert into t_student (name,age) values (?,?);",name,@(arc4random_uniform(40))];

 

        

    }

  

}

 

//查詢

 

-(void)query{

    //1.執行查詢語句

    FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student"];

    //2遍曆結果

    while ([resultSet next]) {

        int ID = [resultSet intForColumn:@"id"];

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

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

        NSLog(@"%d %@ %d",ID,name,age);

    }

    

    

    

}

 

@end

 

iOS 資料庫第三方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.