FMDB的使用, Sqlite常用語句

一.實例化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ù)庫實例?db??這里說明下:如果路徑中不存在"Test.db"的文件,sqlite會自動創(chuàng)建"Test.db"

FMDatabase*db=?[FMDatabasedatabaseWithPath:localDBPath];

if(![dbopen])?{

NSLog(@"數(shù)據(jù)庫未能創(chuàng)建/打開");

returnnil;

}

returndb;

}

也可以用工具在外部設(shè)計好數(shù)據(jù)庫,拖進項目中,項目沒找到數(shù)據(jù)庫時,直接把項目中的數(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ù)庫,就把項目中的數(shù)據(jù)庫拷貝到document中

if(![[NSFileManagerdefaultManager]fileExistsAtPath:localDBPath])?{

NSString*bundleDBPath?=?[[NSBundlemainBundle]pathForResource:@"raoxudong"ofType:@"sqlite"];

[[NSFileManagerdefaultManager]copyItemAtPath:bundleDBPathtoPath:localDBPatherror:nil];

}

//創(chuàng)建數(shù)據(jù)庫實例?db??這里說明下:如果路徑中不存在"Test.db"的文件,sqlite會自動創(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è)置的字段默認是允許為空的,即Allow?Null

*/

//1、創(chuàng)建一個名為table1的表,有三個字段分別為string類型的Name、Age、sex

[dbexecuteUpdate:@"create?table?if?not?exists?table1?(name?text,age?text,sex?text)"];

//2、創(chuàng)建一個名為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可以得到一個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表示降序?,?未明確寫明排序方式時默認是升序)*/

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ù)據(jù)庫一共有多少筆資料:

select count(*) from film;

4、有時候我們只想知道1985年以后的電影有幾部:

select count(*) from film where year >= 1985;

七.增加列,即:增加字段

[objc]view plaincopy

BOOLbool1=?[dbexecuteUpdate:@"alter?table?table2?add?number2?text"];

NSLog(@"add?%@!",bool1?@"success":@"faile");

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容