天天看點

iOS關于資料庫的操作(使用第三方類庫FMDB)

首先要先導入第三方類庫FMdatabase

獲得存放資料庫檔案的沙盒位址

+(NSString *)databaseFilePath

{

NSArray  *filePath =  NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory ,  NSUserDomainMask ,  YES );

NSString  *documentPath = [filePath  objectAtIndex : 0 ];

NSLog ( @"%@" ,filePath);

NSString  *dbFilePath = [documentPath  stringByAppendingPathComponent : @"db.sqlite" ];

return  dbFilePath; 

}

建立資料庫的操作

+(void)creatDatabase

{

db = [[ FMDatabase databaseWithPath :[ self databaseFilePath ]]  retain ];

}

建立表

+(void)creatTable

{

//先判斷資料庫是否存在,如果不存在,建立資料庫 if (! db ) {

[ self creatDatabase ];

}

//判斷資料庫是否已經打開,如果沒有打開,提示失敗 

if (![ db   open ]) {

NSLog ( @"資料庫打開失敗" );

return ;

}

//為資料庫設定緩存,提高查詢效率 

[ db setShouldCacheStatements : YES ];

 //判斷資料庫中是否已經存在這個表,如果不存在則建立該表

if (![ db tableExists : @"people" ])

{

[ db  executeUpdate : @"CREATE TABLES people(people_id INTEGER PRIMARY KEY AUTOINCREAMENT, name TEXT, age INTEGER) " ];

NSLog ( @"建立完成" );

}

}

增加表資料

+(void)insertPeople:(People*)aPeople

{

if (! db ) {

[ self creatDatabase ];

}

if (![ db   open ]) {

NSLog ( @"資料庫打開失敗" );

return ;

}

[ db setShouldCacheStatements : YES ];

if (![ db tableExists : @"people" ])

{

[ self creatTable ];

}

//以上操作與建立表是做的判斷邏輯相同//現在表中查詢有沒有相同的元素,如果有,做修改操作 

FMResultSet *rs = [ db executeQuery : @"select * from people where people_id = ?" ,[ NSString stringWithFormat : @"%d" ,aPeople. peopleID ]];

if ([rs  next ]) {

NSLog ( @"dddddslsdkien" );

[ db executeUpdate : @"update people set name = ?, age = ? where people_id = 1" ,aPeople. name ,[ NSString stringWithFormat : @"%d" ,aPeople. age ]];

}

//向資料庫中插入一條資料 

else {

[ db executeUpdate : @"INSERT INTO people (name, age) VALUES (?,?)" ,aPeople. name ,[ NSString stringWithFormat : @"%d" ,aPeople. age ]];

}

}

删除資料

+(void)deletePeopleByID:(int)ID

{

if (! db ) {

[ self creatDatabase ];

}

if (![ db   open ]) {

NSLog ( @"資料庫打開失敗" );

return ;

}

[ db setShouldCacheStatements : YES ];

 //判斷表中是否有指定的資料, 如果沒有則無删除的必要,直接return

if (![ db tableExists : @"people" ])

{

return ;

}

//删除操作 

[ db  executeUpdate : @"delete from people where people_id = ?" , [ NSString stringWithFormat : @"%d" ,ID]];

[ db   close ];

}

修改操作與增加操作的步驟一緻

查詢

+(NSArray*)getAllPeople

{

if (! db ) {

[ self creatDatabase ];

}

if (![ db   open ]) {

NSLog ( @"資料庫打開失敗" );

return nil ;

}

[ db setShouldCacheStatements : YES ];

if (![ db tableExists : @"people" ])

{

return nil ;

}

//定義一個可變數組,用來存放查詢的結果,傳回給調用者 

NSMutableArray *peopleArray = [[ NSMutableArray alloc ]  initWithArray : 0 ]; //定義一個結果集,存放查詢的資料

FMResultSet *rs = [ db executeQuery : @"select * from people" ]; //判斷結果集中是否有資料,如果有則取出資料

while ([rs  next ]) {

People *aPeople = [[ People   alloc ]  init ];

aPeople. peopleID = [rs  intForColumn : @"people_id" ];

aPeople. name = [rs  stringForColumn : @"name" ];

aPeople. age = [rs  intForColumn : @"age" ];

//将查詢到的資料放入數組中。 

[peopleArray  addObject :aPeople];}

return [peopleArray  autorelease ];

}

原文位址:http://www.cnblogs.com/lzz900201/archive/2012/11/06/2756284.html