一.實(shí)例化FMDatabase
[objc]view plaincopy
//讀取數(shù)據(jù)庫
-(FMDatabase*?)loadDB:(NSString*)dbName
{
//localDBPath:?數(shù)據(jù)庫路徑唆涝,在Document中盯滚。
NSString*localDBPath;
localDBPath=?[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"myTest.sqlite"];
NSLog(@"dbPath:::::%@",localDBPath);
//創(chuàng)建數(shù)據(jù)庫實(shí)例?db??這里說明下:如果路徑中不存在"Test.db"的文件,sqlite會自動(dòng)創(chuàng)建"Test.db"
FMDatabase*db=?[FMDatabasedatabaseWithPath:localDBPath];
if(![dbopen])?{
NSLog(@"數(shù)據(jù)庫未能創(chuàng)建/打開");
returnnil;
}
returndb;
}
也可以用工具在外部設(shè)計(jì)好數(shù)據(jù)庫嫉到,拖進(jìn)項(xiàng)目中哩都,項(xiàng)目沒找到數(shù)據(jù)庫時(shí)税娜,直接把項(xiàng)目中的數(shù)據(jù)庫拷貝到Document中
[objc]view plaincopy
-(FMDatabase*?)loadDB:(NSString*)dbName
{
//localDBPath:?數(shù)據(jù)庫路徑坐搔,在Document中。
NSString*localDBPath;
localDBPath=?[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"myTest.sqlite"];
NSLog(@"dbPath:::::%@",localDBPath);
//如果document中沒找到數(shù)據(jù)庫敬矩,就把項(xiàng)目中的數(shù)據(jù)庫拷貝到document中
if(![[NSFileManagerdefaultManager]fileExistsAtPath:localDBPath])?{
NSString*bundleDBPath?=?[[NSBundlemainBundle]pathForResource:@"raoxudong"ofType:@"sqlite"];
[[NSFileManagerdefaultManager]copyItemAtPath:bundleDBPathtoPath:localDBPatherror:nil];
}
//創(chuàng)建數(shù)據(jù)庫實(shí)例?db??這里說明下:如果路徑中不存在"Test.db"的文件,sqlite會自動(dòng)創(chuàng)建"Test.db"
FMDatabase*db=?[FMDatabasedatabaseWithPath:localDBPath];
if(![dbopen])?{
NSLog(@"數(shù)據(jù)庫未能創(chuàng)建/打開");
returnnil;
}
returndb;
}
二.創(chuàng)建表
[objc]view plaincopy
/*
注意:
在創(chuàng)建table?時(shí)設(shè)置的字段默認(rèn)是允許為空的概行,即Allow?Null
*/
//1、創(chuàng)建一個(gè)名為table1的表弧岳,有三個(gè)字段分別為string類型的Name凳忙、Age、sex
[dbexecuteUpdate:@"create?table?if?not?exists?table1?(name?text,age?text,sex?text)"];
//2禽炬、創(chuàng)建一個(gè)名為table2的表,id字段自增(即:序號)
//not?null表示不用需為空值
[dbexecuteUpdate:@"create?table?if?not?exists?table2?(id?integer?primary?key?AutoIncrement?not?null,name?text,age?text?not?null,sex?text)"];
三.插入
[objc]view plaincopy
BOOLbool2=?[dbexecuteUpdate:@"INSERT?INTO?table1?(name,age,sex)VALUES(?,?,?)",@"name1",@"10",@"男"];
NSLog(@"INSERT?%@!",bool2?@"success":@"faile");
四.更新
[objc]view plaincopy
BOOLbool3=?[dbexecuteUpdate:@"update?table1?set?age=?",@"12"];
NSLog(@"update?%@!",bool3?@"success":@"faile");
BOOLbool4=?[dbexecuteUpdate:@"update?table1?set?age=??where?name=??and?sex=?",@"12",@"name1",@"男"];
NSLog(@"update?%@!",bool4?@"success":@"faile");
五.刪除
[objc]view plaincopy
//刪除數(shù)據(jù)
BOOLbool5=?[dbexecuteUpdate:@"DELETE?FROM?table1?WHERE?name?=??",@"name1"];
NSLog(@"DELETE?%@!",bool5?@"success":@"faile");
六.查詢
取得特定的資料涧卵,則需使用FMResultSet物件接收傳回的內(nèi)容:
[objc]view plaincopy
//用[rs?next]可以輪詢query回來的資料,每一次的next可以得到一個(gè)row裡對應(yīng)的數(shù)值瞎抛,并用[rs?stringForColumn:]或[rs?intForColumn:]等方法把值轉(zhuǎn)成Object-C的型態(tài)艺演。取用完資料后則用[rs?close]把結(jié)果關(guān)閉。
FMResultSet*rs?=?[dbexecuteQuery:@"SELECT?name,?age,?FROM?table1"];
while([rsnext])?{
NSString*name?=?[rsstringForColumn:@"name"];
intage?=?[rsintForColumn:@"age"];
}
[rsclose]
[objc]view plaincopy
#define?QUERY_key(rs,key)?([rs?stringForColumn:key]?[rs?stringForColumn:key]:@"")
FMResultSet*rs=[dbexecuteQuery:@"SELECT?*?FROM?table2?where?sex=??order?by?age?desc",@"男"];
/*(注:?asc?表示升序?,?desc表示降序?,?未明確寫明排序方式時(shí)默認(rèn)是升序)*/
if(rs?!=nil&&[rscolumnCount]>0)?{
while([rsnext])?{
NSMutableDictionary*dic?=?[[NSMutableDictionaryalloc]init];
[dicsetObject:QUERY_key(rs,@"name")forKey:@"name"];
[dicsetObject:QUERY_key(rs,@"age")forKey:@"age"];
[dicsetObject:QUERY_key(rs,@"sex")forKey:@"sex"];
}
}
[dbclose];
[rsclose];
1桐臊、查所有演員名字開頭叫茱蒂的電影('%' 符號便是 SQL 的萬用字符):
select * from film where starring like 'Jodie%';
2胎撤、查所有演員名字以茱蒂開頭、年份晚于1985年断凶、年份晚的優(yōu)先列出伤提、最多十筆,只列出電影名稱和年份:
select title, year from film where starring like 'Jodie%' and year >= 1985 order by year desc limit 10;
3认烁、有時(shí)候我們只想知道數(shù)據(jù)庫一共有多少筆資料:
select count(*) from film;
4肿男、有時(shí)候我們只想知道1985年以后的電影有幾部:
select count(*) from film where year >= 1985;
七.增加列,即:增加字段
[objc]view plaincopy
BOOLbool1=?[dbexecuteUpdate:@"alter?table?table2?add?number2?text"];
NSLog(@"add?%@!",bool1?@"success":@"faile");