使用FMDB進行資料庫操作

2021-07-28 13:44:37 字數 1821 閱讀 7675

fmdb: 

fmdb有三個主要的類

1.fmdatabase – 表示乙個單獨的sqlite資料庫。 用來執行sqlite的命令。

2.fmresultset – 表示fmdatabase執行查詢後結果集

3.fmdatabasequeue – 如果你想在多執行緒中執行多個查詢或更新,你應該使用該類。這是執行緒安全的。

資料庫建立

1.當資料庫檔案不存在時,fmdb會自己建立乙個。

2. 如果你傳入的引數是空串:@"" ,則fmdb會在臨時檔案目錄下建立這個資料庫,資料庫斷開連線時,資料庫檔案被刪除。

3.如果你傳入的引數是 null,則它會建立乙個在記憶體中的資料庫,資料庫斷開連線時,資料庫檔案被刪除。

- (void

)viewdidload else

//建立表

bool

create =  [_db

executeupdate

:@"create table if not exists t_health(id integer primary key  autoincrement, name text,phone text)"

];  

if(create) else

}  [db open]

[db close] 

一切不是select命令的命令都視為更新。這包括  create, update, insert,alter,commit, begin, detach, delete, drop, end, explain,

vacuum, and replace  (等)。

簡單來說,只要不是以select開頭的命令都是update命令。

執行更新返回乙個bool值。yes表示執行成功,否則表示有那些錯誤 。你可以呼叫 -lasterrormessage 和 -lasterrorcode方法來得到更多資訊。

- (ibaction)insert:(uibutton

*)sender else

} - (ibaction)delete

:(uibutton

*)sender else

}  - (ibaction)update:(uibutton

*)sender else

}  

select命令就是查詢,執行查詢的方法是以 -excutequery開頭的。

執行查詢時,如果成功返回fmresultset物件, 錯誤返回nil. 與執行更新相當,支援使用 nserror**引數。同時,你也可以使用 -lasterrorcode和-lasterrormessage獲知錯誤資訊。

為了遍歷查詢結果,你可以使用while迴圈。你還需要知道怎麼跳到下乙個記錄。使用fmdb,很簡單實現,就像這樣

- (ibaction)select:(uibutton

*)sender   

} fmresultset  提供了很多方法來獲得所需的格式的值:

intforcolumn:

longforcolumn:

longlongintforcolumn:

boolforcolumn:

doubleforcolumn:

stringforcolumn:

dataforcolumn:

datanocopyforcolumn:

utf8stringforcolumnindex:

objectforcolumn:

這些方法也都包括 forcolumnindex 的這樣子的方法,引數是查詢結果集的列的索引位置。

iOS 使用FMDB進行資料庫操作(一)

1 首先要先導入第三方類庫fmdatabase。2 獲得存放資料庫檔案的沙盒位址。view row code 1 nsstring databasefilepath 2 3 建立資料庫的操作 view row code 1 void creatdatabase 2 4 建立表 view row co...

OC 資料庫儲存,使用FMDB進行資料庫操作

第一種 乙個資料庫中,同時儲存多張資料表 此處舉例為兩張表 h檔案 import class goodsdetailsvo extern nsstring datastoretype int level extern nsstring tablename inte ce databasecenter...

資料庫操作 使用FMDB

ios中原生的sqlite api在使用上相當不友好,在使用時,非常不便。於是,就出現了一系列將sqlite api進行封裝的庫,例如fmdb plausibledatabase sqlitepersistentobjects等,fmdb 是一款簡潔 易用的封裝庫,這一篇文章簡單介紹下fmdb的使用...