增:四種方式向資料表中插入資料
[cpp]view plain
copy
print?
1)[[dbhelper sharedhelper].db executeupdate:@
"insert into tb_test (username,***) values (?,?)"
, @"lizhongfu"
, @"male"
];
2)[[dbhelper sharedhelper].db executeupdatewithformat:@"insert into tb_test values (%@, %@)"
, @"lzf"
, @"nan"
];
3)[[dbhelper sharedhelper].db executeupdate:@"insert into tb_test (username,***) values (?, ?)"
withargumentsinarray:[nsarray arraywithobjects:@
"zfl"
, @"nanren"
, nil]];
4)nsmutabledictionary *dic = [nsmutabledictionary dictionarywithcapacity:0];
[dic setobject:@"lll"
forkey:@
"username"
];
[dic setobject:@"zzz"
forkey:@
"***"
];
[[dbhelper sharedhelper].db executeupdate:@"insert into tb_test values (:username, :***)"
withparameterdictionary:dic]; 刪:
[cpp]view plain
copy
print?
[[dbhelper sharedhelper].db executeupdate:@
"delete from tb_test where username = ?"
, @"lll"
];
改: 兩種方式更新資料表中資料
[cpp]view plain
copy
print?
1)[[dbhelper sharedhelper].db executeupdate:@
"update tb_test set username = ? where *** = ?"
, @"lizhongfu"
, @"nanren"
];
2)nsmutabledictionary *dic = [nsmutabledictionary dictionarywithcapacity:0];
[dic setobject:@"update_name"
forkey:@
"username"
];
[dic setobject:@"update_***"
forkey:@
"***"
];
[[dbhelper sharedhelper].db executeupdate:@"update tb_test set username = :username, *** = :*** where *** = 'male'"
withparameterdictionary:dic]; 查:
[cpp]view plain
copy
print?
fmresultset *rs = [[dbhelper sharedhelper].db executequery:@
"select * from tb_test"
];
while
([rs next])
例1:[cpp]view plain
copy
print?
fmdatabase* db = [fmdatabase databasewithpath:@
"/tmp/tmp.db"
];
// open 和 close 是成對的 method
if(![db open])
// 為資料庫設定快取,提高查詢效率
[db setshouldcachestatements:yes];
// executeupdate 執行更新 insert updata delete
[db executeupdate:@"create table test (a text, b text, c integer, d double, e double)"
];
// 會執行 begin exclusive transaction 命令鎖住資料庫。
[db begintransaction]; //開始乙個事務
inti = 0;
while
(i++ < 20)
[db commit];//提交這個事務,期間可以rollbacktrans回滾
[db close];
例2:[cpp]view plain
copy
print?
首先要先導入第三方類庫fmdatabase
獲得存放資料庫檔案的沙盒位址
+(nsstring *)databasefilepath
建立資料庫的操作
+(void
)creatdatabase
建立表
+(void
)creattable
//判斷資料庫是否已經開啟,如果沒有開啟,提示失敗
if(![db open])
//為資料庫設定快取,提高查詢效率
[dbsetshouldcachestatements:yes];
//判斷資料庫中是否已經存在這個表,如果不存在則建立該錶
if(![dbtableexists:@
"people"
])
} 增加表資料
+(void
)insertpeople:(people *)apeople
if(![db open])
[dbsetshouldcachestatements:yes];
if(![dbtableexists:@
"people"
])
//以上操作與建立表是做的判斷邏輯相同
//現在表中查詢有沒有相同的元素,如果有,做修改操作
fmresultset *rs = [dbexecutequery:@"select * from people where people_id = ?"
,[nsstringstringwithformat:@
"%d"
,apeople.peopleid]];
if([rs next])
//向資料庫中插入一條資料
else
} 刪除資料
+(void
)deletepeoplebyid:(
int)id
if(![db open])
[dbsetshouldcachestatements:yes];
//判斷表中是否有指定的資料, 如果沒有則無刪除的必要,直接return
if(![dbtableexists:@
"people"
])
//刪除操作
[db executeupdate:@"delete from people where people_id = ?"
, [nsstringstringwithformat:@
"%d"
,id]];
[db close];
} 修改操作與增加操作的步驟一致
查詢
+(nsarray *)getallpeople
if(![db open])
[dbsetshouldcachestatements:yes];
if(![dbtableexists:@
"people"
])
//定義乙個可變陣列,用來存放查詢的結果,返回給呼叫者
nsmutablearray *peoplearray = [[nsmutablearrayalloc] initwitharray:0];
//定義乙個結果集,存放查詢的資料
fmresultset *rs = [dbexecutequery:@"select * from people"
];
//判斷結果集中是否有資料,如果有則取出資料
while
([rs next])
return
[peoplearray autorelease];
}
江湖救急,先整理這麼多~
iOS開發 FMDB使用
demo位址 nslog path database fmdatabase alloc initwithpath path id欄位為自增欄位 name char 256位 age char 3位 char 2位 phone char 13位 address char 100位的 if databa...
ios開發FMDB匯入SQLCipher加密資料庫
工程用得fmdb做資料庫的操作,後期要對資料庫做加密,這裡有兩種方法 1.對資料庫內容加密,存的時候加密,用得時候解密。2.直接對資料庫檔案加密。這裡我選擇了第二種,原因不細說,自己決定。不推薦。後來在fmdb官方發現了這個 即可以用cocoapods來安裝支援sqlcipher加密資料庫的fmdb...
iOS開發 FMDB的使用
獲取沙盒路徑 nsarray filepath nssearchpathfordirectoriesindomains nsdocumentdirectory,nsuserdomainmask,yes nsstring documentpath filepath objectatindex 0 ns...