FMDatabase

FMDatabase 的使用方法


- (NSString*) getPath {

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

   return [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"MyTable"] ;

}



1.创建数据库

-(void)CreateTable;

{

dataBase = [FMDatabasedatabaseWithPath:[selfgetPath]];

    if (![dataBase open])

       NSLog(@"OPEN FAIL");

    }

        

    [dataBaseexecuteUpdate:@"CREATE TABLE IF NOT EXISTS MyTable(aa float,bb text,cc integer,dd integer,ee text)"];

    [dataBaseclose];

}




.查询数据

-(void)QueryData

{

//获取数据

   recordArray = [[NSMutableArrayalloc]init];

    

   db = [[FMDatabasealloc]initWithPath:[selfgetPath]];

    

    if ([db open]) {

       FMResultSet *rs = [dbexecuteQuery:@"SELECT * FROM MyTable"];

        while ([rs next]){

            OneRecord = [[OneRecord alloc]init];

            OneRecord.aa = [NSNumbernumberWithFloat:[rsdoubleForColumn:@"aa"]];

            OneRecord.bb = [rs stringForColumn:@"bb"];

            OneRecord.cc = [NSNumbernumberWithInt:[rsintForColumn:@"cc"]];

            OneRecord.dd = [NSNumbernumberWithInt:[rsintForColumn:@"dd"]];

            OneRecord.ee = [rs stringForColumn:@"ee"];

            [recordArrayaddObject:OneRecord];

            [OneRecordrelease];

        }

        [rs close];

        [dbclose];

}


.更新数据

-(void)UpdateData

{

if ([dbopen]) {

         [dbbeginTransaction];

         [dbexecuteUpdate:@"UPDATE MyTable SET aa = ? WHERE date = ?",aa1,aa2];

         [dbexecuteUpdate:@"UPDATE MyTable SET bb = ? WHERE date = ?",bb1,bb2];

         [dbexecuteUpdate:@"UPDATE MyTable SET cc = ? WHERE date = ?",cc1,cc2];

         [dbexecuteUpdate:@"UPDATE MyTable SET dd = ? WHERE date = ?",dd1,dd2];

         [dbcommit];

         db close];

   }


}


4。插入数据

-(void)insertData

{

//插入数据库

    [dbbeginTransaction];

    [dbexecuteUpdate:@"INSERT INTO MyTable (aa,bb,cc,dd,ee) VALUES (?,?,?,?,?)",

    NSNumber numberWithFloat:aa],bb,cc,dd,ee];

    db commit];

    db close];


}

你可能感兴趣的:(FMDatabase)