iphone數據庫的增刪改查

 

#import "DataBaseOperate.h"


@implementation DataBaseOperate
@synthesize databaseName;


#pragma mark #
#pragma mark DataBase Operate
/*********************************************************************
 函數名稱: init
 函數描述: 初始化數據庫操作 : 打開數據庫
 輸入參數: NULL
 輸出參數: N/A
 返回值  : self
 作   者: 繆小峯
 修改者  :
 備   注: 數據庫 的 關閉在調用方法時關閉( 操作者調用 [XXX close])
 *********************************************************************/

- (id)init{
 [super init];
 if ([self OpenDataBase]) {
  NSLog(@"database opened success!");
 }else {
  NSLog(@"database opened fail!");
 }
 return self;
 
}
/*********************************************************************
 函數名稱: close
 函數描述: 關閉數據庫
 輸入參數: NULL
 輸出參數: N/A
 返回值  : void
 作   者: 繆小峯
 修改者  :
 備   注: 數據庫 的 關閉
 *********************************************************************/
- (void)close{
 sqlite3_close(database);
}
- (void)dealloc{
 self.databaseName=nil;
 [databaseName release];
 [super dealloc];
}
/*********************************************************************
 函數名稱: OpenDataBase
 函數描述: 打開數據庫
 輸入參數: NULL
 輸出參數: bool
 返回值  : BOOL
 作   者: 繆小峯
 修改者  :
 備   注 : 打開數據庫
 *********************************************************************/
-(BOOL)OpenDataBase
{
 databaseName=@"*****.sql";//此處爲數據庫文件名
 NSArray *documentpaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentDir=[documentpaths objectAtIndex:0];
 NSString *databasePath=[documentDir stringByAppendingPathComponent:databaseName];
 NSFileManager *fileManager=[NSFileManager defaultManager];
 BOOL success=[fileManager fileExistsAtPath:databasePath];
 if (!success) {
  NSString *databasePathFromApp=[[[NSBundle  mainBundle]resourcePath]stringByAppendingPathComponent:databaseName];
  [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
 }
 
 if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK)
 {
  return YES; 
 }
 else
 {
  return NO;
 }
 
}

 

#pragma mark #
#pragma mark DataBase bookinfo Operate
/*********************************************************************
 函數名稱: Insert_book
 函數描述: 插入
 輸入參數: id--------------[插入對象]
 輸出參數:
 返回值  : NSInteger-------[返回插入執行的結果,是否成功]
 作   者: 繆小峯
 修改者  :
 備   注 :
 *********************************************************************/
-(NSInteger)Insert_book:(id)anObject{
 if (anObject==nil) {
  return RETURN_TYPE_FAIL;
 }
 BookInfo * _bookinfo=[[BookInfo alloc]init];
 BookInfo * bookinfo = (BookInfo*)anObject;
 
 
 
 //bookName;   //書本名稱
 if (bookinfo.bookName==nil) {
  _bookinfo.bookName=@"";
 }
 else {
  _bookinfo.bookName=bookinfo.bookName;
 }
 NSString *sqlSateMent=[[NSString alloc]initWithFormat:@" insert into book ( bookName) values ('%@')",_bookinfo.bookName];
 
 char *errmsg=NULL;
 [_bookinfo release];
 
 if (sqlite3_exec(database, [sqlSateMent UTF8String], 0, nil, &errmsg) != SQLITE_OK) {
  NSLog(@"%@",[NSString stringWithFormat:@"insert %@ fail:%@",listname,sqlSateMent]);
  [sqlSateMent release];
  return RETURN_TYPE_FAIL;
 }
 [sqlSateMent release];
 NSLog(@"%@",[NSString stringWithFormat:@"insert %@ success!",listname]);
 return RETURN_TYPE_SUCCESS;
 
}
/*********************************************************************
 函數名稱: Update_bookinfo
 函數描述: 更新書本信息
 輸入參數: id--------------[插入對象]
 輸出參數:
 返回值  : NSInteger-------[返回插入執行的結果,是否成功]
 作   者: 繆小峯
 修改者  :
 備   注 :
 *********************************************************************/
-(NSInteger)Update_bookinfo:(id)anObject{
 if (anObject==nil) {
  return RETURN_TYPE_FAIL;
 }
 BookInfo * bookinfo = (BookInfo *)anObject;
 
 
 NSString *sqlSateMent=[[NSString alloc]initWithFormat:@" update  book set bookName='%@'", bookinfo.bookName];
 
 
 char *errmsg=NULL;
 if (sqlite3_exec(database, [sqlSateMent UTF8String], 0, nil, &errmsg) != SQLITE_OK) {
  NSLog(@"%@",[NSString stringWithFormat:@"update %@ fail:%@",listname,sqlSateMent]);
  [sqlSateMent release];
  return RETURN_TYPE_FAIL;
 }
 NSLog(@"%@",[NSString stringWithFormat:@"update %@ success!",listname]);
 [sqlSateMent release];
 return RETURN_TYPE_SUCCESS;
}
/*********************************************************************
 函數名稱: Delete_By
 函數描述: 條件刪除
 輸入參數: NSString--------------[刪除條件]
 輸出參數:
 返回值  : NSInteger-------[返回刪除執行的結果]
 作   者: 繆小峯
 修改者  :
 備   注 :
 *********************************************************************/
-(NSInteger)Delete_By:(NSString *)where {
 
 NSString *sqlSateMent=[[NSString alloc]initWithFormat:@" delete from book where %@",where];
 char *errmsg=NULL;
 if (sqlite3_exec(database, [sqlSateMent UTF8String], 0, nil, &errmsg) != SQLITE_OK) {
  NSLog(@"%@",[NSString stringWithFormat:@"delete  %@ fail:%@",listname,sqlSateMent]);
  [sqlSateMent release];
  return RETURN_TYPE_FAIL;
 }
 NSLog(@"%@",[NSString stringWithFormat:@"delete   %@ success!:%@",listname,where]);
 [sqlSateMent release];
 return RETURN_TYPE_SUCCESS;
}


/*********************************************************************
 函數名稱: GetListBy_bookinfo
 函數描述: 條件檢索
 輸入參數: NSString--------------[檢索條件]
 輸入參數: NSString-------[返回集合的排序規則]
 輸出參數:
 返回值  : NSArray-------[返回檢索的結果]
 作   者: 繆小峯
 修改者  :
 備   注 :
 *********************************************************************/
-(NSArray *)GetListBy_bookinfo:(NSString *)where Order:(NSString *)srot {
 //define return value
 NSMutableArray *bookInfoArr=[[[NSMutableArray alloc] init] autorelease];
 
 
 //generate search SQL
 NSString * sqlStateMent;
 
 NSString * _sqlhead ;
 if ([where length]>0) {
  _sqlhead=[[NSString alloc]initWithFormat:@"  select * from book where %@",where];
 }else {
  _sqlhead=[[NSString alloc]initWithFormat:@"  select * from book  "];
 }
 
 
 sqlStateMent=[_sqlhead stringByAppendingString:srot];
 [_sqlhead release];
 
 
 
 sqlite3_stmt *complitedSateMent;
 //exec SQL
 if (sqlite3_prepare_v2(database,[sqlStateMent UTF8String],-1,&complitedSateMent,NULL)==SQLITE_OK) {
  while (sqlite3_step(complitedSateMent)==SQLITE_ROW) {
   
   BookInfo * bookinfo = [[BookInfo alloc] init];
   
   

   //書本名稱
   if ((char *)sqlite3_column_text(complitedSateMent,0 )==nil) {
    bookinfo.bookName=@"";
   }
   else {
    bookinfo.bookName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(complitedSateMent,0 )];
   }
   
   //Data Package   
   
   [bookInfoArr addObject:bookinfo];
   [bookinfo release];
   bookinfo = nil;
   
   
   
  }
  //finalize stmt  
  sqlite3_finalize(complitedSateMent);
 }
 
 
 
 //end & return value
 return bookInfoArr;
}
/*********************************************************************
 函數名稱: GetExistBy_bookinfo
 函數描述: 檢索該對象是否存在
 輸入參數: id--------------[插入對象]
 輸出參數:
 返回值  : NSInteger-------[返回插入執行的結果,0/1]
 作   者: 繆小峯
 修改者  :
 備   注 :
 *********************************************************************/
-(NSInteger)GetExistBy_bookinfo:(id)anObject {
 NSInteger retResult=0;
 BookInfo *_bookinfo=(BookInfo *)anObject;
 NSString *sqlStateMent=[[NSString alloc]initWithFormat:@" select count(bookName) from book where bookName='%@' ",_bookinfo.bookName];
 sqlite3_stmt *complitedSateMent;
 //exec SQL
 if (sqlite3_prepare_v2(database,[sqlStateMent UTF8String],-1,&complitedSateMent,NULL)==SQLITE_OK) {
  while (sqlite3_step(complitedSateMent)==SQLITE_ROW) {
   retResult=sqlite3_column_int(complitedSateMent,0);
   break;
  }
 }
 [sqlStateMent release];
 sqlite3_finalize(complitedSateMent);
 return retResult;
}
@end

發佈了13 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章