天天看點

iOS學習——FMDB詳解介紹GitHub安裝使用

歡迎到我的個人域名部落格:http://zhoulingyu.com

介紹

FMDB現在越來越多的被使用了,作為輕量級資料庫架構,FMDB對sqlite做了簡單的包裝,使得使用更簡單。

但是小魚認為FMDB還是不好用,畢竟不能做到像hebernate一樣傳一個對象直接儲存,直接省去sql語句。之後小魚打算自己動封裝一個架構,至少能夠對簡單的entity進行一鍵增删改查。

GitHub

FMDB連結:https://github.com/ccgus/fmdb

安裝

1. 使用CocoaPods安裝

pods安裝當然是最簡單的,在Podfile檔案中加入

然後終端進入項目根目錄,輸入

等待安裝完就好了

如果不會使用CocoaPods,可以參考我的這篇博文:iOS學習——CocoaPods的使用

2. 傳統拖檔案安裝

在github上下載下傳FMDB,把這些檔案直接拖入你的項目就可以用了

iOS學習——FMDB詳解介紹GitHub安裝使用

使用

首先要在用到的地方

建立資料庫

如果該位置沒有此資料庫檔案将會建立一個新的資料庫檔案,如果已經有了,将會直接獲得該資料庫。

dataBasePath可以這樣設定:

#define dataBasePath [[(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)) lastObject]stringByAppendingPathComponent:dataBaseName]
#define dataBaseName @"test.sqlite"
           

打開資料庫

傳回BOOL類型,代表是否成功

[db open]
           

關閉資料庫

傳回BOOL類型,代表是否成功

[db close]
           

建表

if ([self.db open]) {
    NSString *sqlCreateTable = @"CREATE TABLE IF NOT EXISTS Question (UUID TEXT PRIMARY KEY, type TEXT, bigQuetionIndex INTEGER, smallQuetionIndex INTEGER, state TEXT, time INTEGER, elapsedTime INTEGER, studentAnswer TEXT, answer TEXT, studentGrade INTEGER, grade INTEGER, content TEXT, examPaperUUID TEXT)";

    BOOL res = [self.db executeStatements:sqlCreateTable];
    if (!res) {
        CFLog(@"error when creating db table");
    } else {
        CFLog(@"success to creating db table");
    }
    [self.db close];
}
           

增加資料

if ([self.db open]) {
     NSString *sql = @"INSERT INTO t_entity (UUID, title) VALUES (?, ?)";

        BOOL res = [self.db executeUpdate:sql, entity.UUID, entity.title];

        if (!res) {
            CFLog(@"error when insert db table");
        } else {
            CFLog(@"success to insert db table");
    }
    [self.db close];
}
           

删除資料

if ([self.db open]) {
        NSString *sql = @"DELETE FROM t_entity WHERE UUID = ?";
        BOOL res = [self.db executeUpdate:sql, UUID];

        if (!res) {
            CFLog(@"error when DELETE db table");
        } else {
            CFLog(@"success to DELETE db table");
        }
        [self.db close];
}
           

更新資料

if ([self.db open]) {
        NSString *sql = @"UPDATE t_entity SET title = ?";
        BOOL res = [self.db executeUpdate:sql,
                    examPaper.title,
                    examPaper.UUID];
        if (!res) {
            CFLog(@"error when UPDATE db table");
        } else {
            CFLog(@"success to UPDATE db table");
        }
        [self.db close];
}
           

查找資料

Entity *entity;
if ([self.db open]) {
    //傳回資料庫中第一條滿足條件的結果
    NSString *sql = @"SELECT * FROM t_entity WHERE UUID = ?";

    //傳回全部查詢結果
    FMResultSet *rs=[self.db executeQuery:sql, UUID, nil];

    while ([rs next]){
        entity = [[Entity alloc] init];
        entity.UUID = [rs stringForColumn:@"UUID"];
        entity.type = [rs stringForColumn:@"type"];
    }

    [rs close];
    [self.db close];
}
           

重要提示:使用占位符『?』

我在網上看到很多人是這樣用的

錯誤代碼:

NSString *sql = [NSString stringWithFormat: @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')", TABLENAME, NAME, AGE, ADDRESS, @"value1", @"value2", @"value3"];  
BOOL res = [db sql]; 
           

這是

錯誤

的,在官方文檔上已經指出:

you SHOULD NOT do this (or anything like this):

Instead, you SHOULD do:

也就是說不應該使用拼串的方式,而應該使用

占位符

重要提示:使用NSNumber儲存

官方文檔指出,用int儲存是

不起作用

的:

正确

的做法是這樣的: