前言
如果不是為了深入了解FMDB,只求能用就行,只要記住下面這句話就行了:在FMDB中饲齐,除了查詢以外的所有操作都稱為'更新'(create、drop咧最、insert捂人、update、delete等)
啥也別說了矢沿,直接介紹FMDB滥搭。
FMDB介紹
1. 什么是FMDB?
FMDB是iOS開發(fā)平臺(tái)的SQLite數(shù)據(jù)庫框架
FMDB是以O(shè)C的封裝封裝了SQLite的C語言API
2. FMDB優(yōu)點(diǎn)
使用起來更加面向?qū)ο笞刹欤∪チ撕芏嗦闊┞畚酢⑷哂嗟腃語言代碼
與蘋果自帶的Core Data框架相比更加的輕量和靈活
提供了多線程安全的數(shù)據(jù)庫操作方法,有效的防止數(shù)據(jù)混亂
3. GitHub
4. 導(dǎo)入
導(dǎo)入什么的可以自己去GitHub上下載然后手動(dòng)導(dǎo)入摄狱,也可以直接使用cocoaPod自動(dòng)導(dǎo)入脓诡,詳細(xì)的步驟請(qǐng)移步FMDB官方GitHub。
5. 編碼之前
在編碼之前媒役,我們很有必要先把導(dǎo)入進(jìn)來的FMDB庫給大致瀏覽一下祝谚。
從圖中我們可以知道,F(xiàn)MDB由FMDatabase
酣衷、FMDatabaseAdditions
交惯、FMDatabasePool
、FMDatabaseQueue
、FMResultSet
五個(gè)類組成席爽。大概瀏覽的話 直接只看.h
中的內(nèi)容就可以了意荤,至于實(shí)現(xiàn)就不用看了。
主要的核心類有三個(gè):
FMDatabase
一個(gè)FMDatabase對(duì)象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫只锻,這個(gè)類主要是用來創(chuàng)建數(shù)據(jù)庫玖像、創(chuàng)建表、增刪改查數(shù)據(jù)等功能的齐饮,簡(jiǎn)單概括來說就是執(zhí)行SQL語句的捐寥。FMResultSet
這個(gè)類的主要作用是用來保存執(zhí)行查詢SQL的結(jié)果。FMDatabaseQueue
用于在多線程中執(zhí)行多個(gè)查詢或者更新祖驱,它是線程安全的
FMDB編碼
有計(jì)算機(jī)基礎(chǔ)的都知道握恳,要想進(jìn)行數(shù)據(jù)庫操作,首先要有那么一個(gè)能夠讓你操作的數(shù)據(jù)庫(系統(tǒng)級(jí)別的數(shù)據(jù)庫一般是禁止修改的)捺僻,有了可供修改的數(shù)據(jù)庫之后乡洼,我們就可以建表插入數(shù)據(jù)了。(說句廢話:數(shù)據(jù)的存儲(chǔ)是建立在數(shù)據(jù)表的基礎(chǔ)上陵像,不是直接插入到數(shù)據(jù)庫的)
從上面加粗的文字中我們就知道了操作數(shù)據(jù)庫的步驟了:
1. 建立數(shù)據(jù)庫
通過制定SQLite數(shù)據(jù)庫文件路徑來創(chuàng)建FMDatabase對(duì)象就珠。我們這里就將數(shù)據(jù)庫的位置定為沙盒中的tmp了(慎重,我這里只是為了演示醒颖,在特定情況下妻怎,這里的數(shù)據(jù)會(huì)被系統(tǒng)回收的),數(shù)據(jù)庫名字定為tmp泞歉,后綴必須是db逼侦。
// 創(chuàng)建數(shù)據(jù)庫
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp.db"];
self.db = [FMDatabase databaseWithPath:path];
2. 打開/關(guān)閉數(shù)據(jù)庫
就像看書一樣,有了可以看的書腰耙,接下來的動(dòng)作就是看書咯榛丢;同理,有了我們自己的數(shù)據(jù)庫挺庞,想要操作就必須先打開數(shù)據(jù)庫建立連接晰赞。
// 打開數(shù)據(jù)庫
if (![self.db open]) {
self.db = nil;
NSLog(@"數(shù)據(jù)庫打開失敗");
return;
}
在不使用數(shù)據(jù)庫的時(shí)候我們就可以關(guān)閉數(shù)據(jù)庫連接
[self.db close];
3. 操作數(shù)據(jù)庫
在本篇文章開篇的時(shí)候我已經(jīng)說了,在FMDB中选侨,除了查詢以外的所有操作都稱為'更新'(create掖鱼、drop、insert援制、update戏挡、delete等),所以在操作數(shù)據(jù)庫這一步我們只需要分為更新和查詢兩種情況就可以了晨仑。
1. 更新
在FMDB中褐墅,除了查詢以外的其他所有操作拆檬,都可以稱之為“更新”
使用executeUpdate:方法執(zhí)行更新:
- (BOOL)executeUpdate:(NSString*)sql, ...
- (BOOL)executeUpdateWithFormat:(NSString*)format, ...
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments
- 創(chuàng)建表
創(chuàng)建表sql:create table
表名
(字段1, 字段2, 字段3, ...);
這里我們指定要?jiǎng)?chuàng)建的表的名字為t_student
,表中包含有id
(主鍵妥凳、自增竟贯、integer類型)、name
(學(xué)生姓名)逝钥、age
(學(xué)生年齡)這三個(gè)字段澄耍,創(chuàng)建表代碼如下:
NSString *sql = @"create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer not null)";
if ([self.db executeUpdate:sql]) {
NSLog(@"創(chuàng)建表t_student成功");
}else {
NSLog(@"創(chuàng)建表t_student失敗");
}
或者
if ([self.db executeUpdate:@"create table if not exists t_students (id integer primary key autoincrement, name text not null, age integer not null);" withArgumentsInArray:@[]]) {
NSLog(@"創(chuàng)建表成功");
}else {
NSLog(@"創(chuàng)建表失敗");
}
亦或者
if ([self.db executeUpdateWithFormat:@"create table if not exists t_students (id integer primary key autoincrement, name text not null, age integer not null);"]) {
NSLog(@"創(chuàng)建表成功");
}else {
NSLog(@"創(chuàng)建表失敗");
}
這里主要是在sql語句中沒有參數(shù),所以三種方法的區(qū)別不明顯晌缘,在下面的增刪改中的示例你就可以看出來這幾種的區(qū)別了。
- 新增數(shù)據(jù)
新增數(shù)據(jù)sql:insert into
表名
(字段1, 字段2, ...) values (值1, 值2, ...);
這里的字段1和值1一定要一一對(duì)應(yīng)
我們新增一條張三同學(xué)的信息痢站。
姓名:張三 年齡:20
executeUpdate: 函數(shù)參數(shù)sql語句中不確定的參數(shù)使用
?
來占位磷箕。
executeUpdateWithFormat: 函數(shù)參數(shù)sql語句中不確定的參數(shù)使用%@
、%d
等來占位阵难。
NSString *name = @"張三";
int age = 20;
// 這里的age參數(shù)岳枷,也可以寫為這樣的形式:@(age),但是絕對(duì)不能直接傳int類型或者integer類型呜叫,那樣運(yùn)行會(huì)報(bào)錯(cuò)的空繁。
[self.db executeUpdate:@"insert into t_student (name, age) values (?, ?)", name, [NSNumber numberWithInt:age]];
或者
NSString *name = @"張三";
int age = 20;
[self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %@)", name, @(age)];
亦或者
NSString *name = @"張三";
int age = 20;
[self.db executeUpdate:@"insert into t_student (name, age) values (?, ?)" withArgumentsInArray:@[name, @(age)]];
- 修改數(shù)據(jù)
更改sql:update
表名
set列名
=新值
where列名
=某值
我們修改剛才插入的那條數(shù)據(jù),將其年齡改為10歲
NSString *name = @"張三";
int ageNew = 10;
// 這里是以參數(shù)的形式展示朱庆,也可以用注釋中的那種方式
// [self.db executeUpdate:@"update t_student set age = 10 where name = '張三'"];
[self.db executeUpdate:@"update t_student set age = ? where name = ?", @(ageNew), name];
或者
NSString *name = @"張三";
int ageNew = 10;
[self.db executeUpdateWithFormat:@"update t_student set age = %@ where name = %@", @(ageNew), name];
亦或者
NSString *name = @"張三";
int ageNew = 10;
[self.db executeUpdate:@"update t_student set age = ? where name = ?" withArgumentsInArray:@[@(ageNew), name]];
- 刪除數(shù)據(jù)
刪除sql:delete from
表名
where列名
=某值
刪除數(shù)據(jù)可以單獨(dú)刪除一條數(shù)據(jù)或者是把表中的數(shù)據(jù)全部都刪除
刪除一條數(shù)據(jù)
// 刪除t_student表中的某一條數(shù)據(jù)
[self.db executeUpdate:@"delete from t_student where name = '張三'"];
刪除t_student表中所有的數(shù)據(jù)
// 刪除t_student表中的所有數(shù)據(jù)
[self.db executeUpdate:@"delete from t_student"];
- 刪除表
刪除表sql:drop table
表名
刪除t_student表
[self.db executeUpdate:@"drop table t_student"];
2. 查詢
查詢方法:
- (FMResultSet *)executeQuery:(NSString*)sql, ...
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
- 查詢t_student表中的所有數(shù)據(jù)
查詢sql:select
列名
from表名
的所有值
以及
select * from表名
查詢t_student表中的所有的學(xué)生名字
// 查詢t_student表中的學(xué)生名字
// 執(zhí)行查詢語句
FMResultSet *result = [self.db executeQuery:@"select name from t_student"];
// 遍歷查詢結(jié)果
while ([result next]) {
NSLog(@"name: %@", [result stringForColumn:@"name"]);
}
查詢t_student表中的所有數(shù)據(jù)
// 查詢t_student表中的內(nèi)容
FMResultSet *result = [self.db executeQuery:@"select * from t_student"];
while ([result next]) {
NSLog(@"id: %d name: %@ age: %d", [result intForColumnIndex:0], [result stringForColumn:@"name"], [result intForColumnIndex:2]);
}
不熟悉或者不會(huì)數(shù)據(jù)庫增刪改查的同學(xué)以及想要學(xué)習(xí)更復(fù)雜的sql語句的可以移步W3School進(jìn)行深入學(xué)習(xí)盛泡。
友情鏈接:W3School