在iOS開發中使用FMDB

2021-06-19 09:38:03 字數 4371 閱讀 2464

sqlite ( 是乙個輕量級的關聯式資料庫。ios sdk很早就支援了sqlite,在使用時,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 標頭檔案即可。但是,原生的sqlite api在使用上相當不友好,在使用時,非常不便。於是,開源社群中就出現了一系列將sqlite api進行封裝的庫,而fmdb ( 則是開源社群中的優秀者。

fmdb在使用上相當方便。以下是乙個簡單的例子:

123

4567

891011

nsstring

*docsdir=[

nssearchpathfordirectoriesindomains

(nsdocumentdirectory

,nsuserdomainmask

,yes

)lastobject

];nsstring

*dbpath=[

docsdir

@"user.sqlite"

];fmdatabase*db

=[fmdatabase

databasewithpath:

dbpath];[

dbopen

];fmresultset*rs

=[dbexecutequery:

@"select * from people"

];while

([rs

next])[

dbclose

];

可以看到,使用fmdb後的資料庫**清晰明了,比原生的api優雅多了。另外,fmdb同時相容arc和非arc工程,會自動根據工程配置來調整相關的記憶體管理**。

該使用說明主要翻譯自fmdb的github專案說明文件: 

首先將fmdb從github上clone下來,然後將以下檔案copy到你的工程中:

123

4567

8910

fmdatabase.h

fmdatabase.m

fmdatabaseadditions.h

fmdatabaseadditions.m

fmdatabasepool.h

fmdatabasepool.m

fmdatabasequeue.h

fmdatabasequeue.m

fmresultset.h

fmresultset.m

建立資料庫只需要如下一行即可,當該檔案不存在時,fmdb會自己建立乙個。如果你傳入的引數是空串:@」」 ,則fmdb會在臨時檔案目錄下建立這個資料庫,如果你傳入的引數是 null,則它會建立乙個在記憶體中的資料庫。

1

fmdatabase*db

=[fmdatabase

databasewithpath:

@"/tmp/tmp.db"

];

使用如下語句,如果開啟失敗,可能是許可權不足或者資源不足。通常開啟完操作操作後,需要呼叫close方法來關閉資料庫。

123

4567

8

if(!

[dbopen

])// some operation

// ...[db

close

];

除了select操作之外,其它的都是更新操作。更新操作使用如下方法,如果有錯誤,可以用error引數中獲得。

1

-

[fmdatabase

executeupdate:error:withargumentsinarray:orvalist:

]

查詢操作示例如下。注意:即使操作結果只有一行,也需要先呼叫fmresultset的next方法。

123

4567

89

fmresultset*s

=[dbexecutequery:

@"select * from mytable"

];while([s

next

])fmresultset*s

=[dbexecutequery:

@"select count(*) from mytable"

];if([s

next

])

fmdb提供如下多個方法來獲取不同型別的資料:

123

4567

891011

intforcolumn:

longforcolumn:

longlongintforcolumn:

boolforcolumn:

doubleforcolumn:

stringforcolumn:

dateforcolumn:

dataforcolumn:

datanocopyforcolumn:

utf8stringforcolumnindex:

objectforcolumn:

通常情況下,你並不需要關閉fmresultset,因為相關的資料庫關閉時,fmresultset也會被自動關閉。

通常情況下,你可以按照標準的sql語句,用?表示執行語句的引數,如:

1

insert

into

mytable

values(?

,?,?

)

然後,可以我們可以呼叫executeupdate方法來將?所指代的具體引數傳入,通常是用變長引數來傳遞進去的,如下:

1

2

nsstring

*sql

=@"insert into user (name, password) values (?, ?)";[

dbexecuteupdate:

sql,

user

.name

,user

.password

];

這裡需要注意的是,引數必須是nsobject的子類,所以象int,double,bool這種基本型別,需要封裝成對應的包裝類才行,如下所示:

123

4

// 錯誤,42不能作為引數[db

executeupdate:

@"insert into mytable values (?)",42

];// 正確,將42封裝成 nsnumber 類[db

executeupdate:

@"insert into mytable values (?)",[

nsnumber

numberwithint:

42]];

使用fmdatabasequeue很簡單,首先用乙個資料庫檔案位址來初使化fmdatabasequeue,然後就可以將乙個閉包(block)傳入indatabase方法中。 在閉包中運算元據庫,而不直接參與fmdatabase的管理。

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

28

// 建立,最好放在乙個單例的類中

fmdatabasequeue

*queue=[

fmdatabasequeue

databasequeuewithpath:

apath

];// 使用

[queue

indatabase:^(

fmdatabase*db

)}];

// 如果要支援事務

[queue

intransaction:^(

fmdatabase*db

,bool

*rollback

)// etc…[db

executeupdate:

@"insert into mytable values (?)",[

nsnumber

numberwithint:

4]];

}];

在iOS開發中使用FMDB

sqlite 是乙個輕量級的關聯式資料庫。ios sdk很早就支援了sqlite,在使用時,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 標頭檔案即可。但是,原生的sqlite api在使用上相當不友好,在使用時,非常不便。於是,開源社群中就出現了一系列將sqlit...

在iOS開發中使用FMDB

sqlite 是乙個輕量級的關聯式資料庫。ios sdk很早就支援了sqlite,在使用時,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 標頭檔案即可。但是,原生的sqlite api在使用上相當不友好,在使用時,非常不便。於是,開源社群中就出現了一系列將sqlit...

在iOS開發中使用FMDB

sqlite 是乙個輕量級的關聯式資料庫。ios sdk很早就支援了sqlite,在使用時,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 標頭檔案即可。但是,原生的sqlite api在使用上相當不友好,在使用時,非常不便。於是,開源社群中就出現了一系列將sqlit...