FMDatabase的用法介紹

2021-06-18 14:13:04 字數 3804 閱讀 3383

本人以後都會不定時的寫一些關於ios開發的教程,大家希望可提些意見,大家希望寫些什麼,對大家有幫助的,希望幫到大家:

fmdatabase其實是乙個很輕級的sqlite資料庫封裝庫,用object-c封裝c,它主要有2個類:fmdatabase和fmresultset

它主要有這些檔案

然後,就需要新增sqlite框架   

這樣,你的工程就能開始使用fmdatabase了。

下面就介紹一下它的用法:

1.首先我們需要建立乙個fmdatabase例項:

+(fmdatabase*)databasesigoninstance

2.接下來就是需要建立乙個資料表了:

//開啟資料庫

fmdatabase*db = [fmdatabasepathdatabasesigoninstance];

if (!db.open)

db.logserrors =yes;//

開啟錯誤日誌

[db 

executeupdate

:@"create table if not exists studenttable(name varchar,age integer,totalscore float)"];

[dbclose];

3.新增資料:

fmdatabase*db = [fmdatabasepathdatabasesigoninstance];

if (!db.open)

//獲得資料

student.name= ((uitextfield*)[alertviewviewwithtag:

textfield_tag

]).text;

student.age= [((uitextfield*)[alertviewviewwithtag:

textfield_tag

+ 1]).textintvalue];

student.totalscore= [((uitextfield*)[alertviewviewwithtag:

textfield_tag

+ 2]).textfloatvalue];

db.logserrors =yes;

[db executeupdate:

@"insert into studenttable(name,age,totalscore)values(?,?,?)"

,student.name,[nsnumbernumberwithint:student.age],[nsnumbernumberwithfloat:student.totalscore]];

//注意引數必須是物件

[dbclose];

4.刪除資料:

fmdatabase*db = [fmdatabasepathdatabasesigoninstance];

if (!db.open)

db.logserrors =yes;

nsstring

*deldatasqlstr =@"delete from studenttable";

[dbexecuteupdate:deldatasqlstr];

[dbclose];

5.修改資料:

fmdatabase*db = [fmdatabasepathdatabasesigoninstance];

if (!db.open)

db.logserrors =yes;

[db 

executeupdate

:@"update studenttable set totalscore = 100"];

[dbclose];

查詢資料:

fmdatabase*db = [fmdatabasepathdatabasesigoninstance];

if (!db.open)

db.logserrors =yes;

fmresultset

*rs = [db

executequery

:@"select * from studenttable"];

nsmutablearray*studentarr = [[nsmutablearrayalloc]init];

while ([rsnext])

for (int count = 0; count< [studentarrcount]; count++)

[studentarrrelease];

[dbclose];

上面的都是一些增刪改查的用法

下面我們再來看看fmdatabase 和

fmresultset

fmdatabase:(主要用到這兩個)

- (bool)executeupdate:(nsstring*)sql, ...;   這個是用於執行無結果的sql語句的

- (fmresultset *)executequery:(nsstring*)sql, ...;  這個是用於執行有結果的sql語句的

fmresultset:

- (bool)next; 結果集的下乙個

- (int)columncount; 結果集的字段個數

- (int)intforcolumn:(nsstring*)columnname; 獲取整形欄位的資訊(下面的都是同理)

- (int)intforcolumnindex:(int)columnidx;

- (long)longforcolumn:(nsstring*)columnname;

- (long)longforcolumnindex:(int)columnidx;

- (long

long

int)longlongintforcolumn:(nsstring*)columnname;

- (long

long

int)longlongintforcolumnindex:(int)columnidx;

- (unsigned

long

long

int)unsignedlonglongintforcolumn:(nsstring*)columnname;

- (unsigned

long

long

int)unsignedlonglongintforcolumnindex:(int)columnidx;

- (bool)boolforcolumn:(nsstring*)columnname;

- (bool)boolforcolumnindex:(int)columnidx;

- (double)doubleforcolumn:(nsstring*)columnname;

- (double)doubleforcolumnindex:(int)columnidx;

- (nsstring*)stringforcolumn:(nsstring*)columnname;

- (nsstring*)stringforcolumnindex:(int)columnidx;

- (nsdate*)dateforcolumn:(nsstring*)columnname;

- (nsdate*)dateforcolumnindex:(int)columnidx;

- (nsdata*)dataforcolumn:(nsstring*)columnname; 獲取二進位制資料(可以使用這個)

- (nsdata*)dataforcolumnindex:(int)columnidx;

FMDatabase 的使用方法

ios fmdatabase的使用方法 以下是fmdb的一些基本使用,fmdb框架其實只是一層很薄的封裝,主要的類也就兩個 fmdatabase和fmresultset 其中的fmresultset物件讓我想起了android中sqlite的cursor集合啊。fmdb的github位址是,補充 是...

MySQL exists的用法介紹

有乙個查詢如下 1selectc.customerid,companyname 2fromcustomers c 3whereexists 4selectorderidfromorders o 5whereo.customerid cu.customerid 這裡面的exists是如何運作呢?子查詢...

MySQL exists的用法介紹

有乙個查詢如下 select c.customerid,companyname from customers c where exists select orderid from orders o where o.customerid cu.customerid 這裡面的exists是如何運作呢?子...