使用FMDB 基本操作

2021-06-28 22:58:16 字數 1428 閱讀 9579

建立,插入,更新和刪除:使用executeupdate方法,而查詢則用executequery

1.例項化fmdatabase

//paths: ios下document路徑,document為ios中可讀寫的資料夾

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsstring *documentdirectory = [paths objectatindex:0];

//建立資料庫例項 db  這裡說明下:如果路徑中不存在"test.db"的檔案,sqlite會自動建立"test.db"

fmdatabase *db= [fmdatabase databasewithpath:dbpath] ;

if (![db open])

2.建立表

//建立乙個名為user的表,有兩個字段分別為string型別的name,integer型別的 age

[db executeupdate:@"create table user (name text,age integer)"];

3.插入

//插入資料使用oc中的型別 text對應為nsstring integer對應為nsnumber的整形

[db executeupdate:@"insert into user (name,age) values (?,?)",@"張三",[nsnumber numberwithint:20]];

4.更新

//更新資料 將「張三」更改為「李四」

[db executeupdate:@"update user set name = ? where name = ? ",@"李四",@"張三"];

5.刪除

//刪除資料

[db executeupdate:@"delete from user where name = ?",@"張三"];

6.查詢

//返回資料庫中第一條滿足條件的結果

nsstring *aa=[db stringforquery:@"select name from user where age = ?",@"20"];

//返回全部查詢結果

fmresultset *rs=[db executequery:@"select * from user"];

rs=[db executequery:@"select * from user where age = ?",@"20"];

while ([rs next])

[rs close];

FMDB基本操作

1.例項化fmdatabase paths ios下document路徑,document為ios中可讀寫的資料夾 nsarray paths nssearchpathfordirectoriesindomains nsdocumentdirectory,nsuserdomainmask,yes n...

使用FMDB 1 基本操作

1.例項化fmdatabase paths ios下document路徑,document為ios中可讀寫的資料夾 nsarray paths nssearchpathfordirectoriesindomains nsdocumentdirectory,nsuserdomainmask,yes n...

使用FMDB 1 基本操作

建立,插入,更新和刪除 使用executeupdate方法,而查詢則用executequery 1.例項化fmdatabase paths ios下document路徑,document為ios中可讀寫的資料夾 nsarray paths nssearchpathfordirectoriesindo...