从解析的数组中将数据插入数据库的问题

时间:2012-07-19 21:04:46

标签: iphone objective-c ios sqlite

-(void)saveTheData:(InternetResource*)_resource
{
    currentResultsList= [[[NSMutableArray alloc] initWithArray:[iconnect.responseString JSONValue]] mutableCopy];
    //NSLog(@"Response String:  %@",iconnect.responseString);
    NSLog(@"bla bla%@",currentResultsList);
    NSString *databaseName = @"olympics_db3";//rodeo_database_details //rodeo_user_database
    NSLog(@"dsdsd %@",databaseName);
    NSString *path =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
}

sqlite3_stmt    *statement;
const char *dbpath = [path UTF8String];


if (sqlite3_open(dbpath,&sql) == SQLITE_OK)
{  
    NSInteger athleteID;
    NSInteger countryMedal;
    NSInteger eventID;
    NSString *result;
    NSInteger resultID;
    NSInteger sportID;
    NSInteger typeOfMedal;

    for(int i=0;i<[currentResultsList count];i++)
    {
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"athleteID"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"athleteID"] length]==0)
        {
            athleteID=0;    

        }
        else
        {
             athleteID=[[[currentResultsList objectAtIndex:i] objectForKey:@"athleteID"] integerValue];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"countryMedal"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"countryMedal"] length]==0)
        {
            countryMedal=0;
        }
        else
        {
            countryMedal=[[[currentResultsList objectAtIndex:i]objectForKey:@"countryMedal"]integerValue];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"eventID"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"eventID"] length]==0)
        {
            eventID=0;
        }
        else
        {
        eventID= [[[currentResultsList objectAtIndex:i] objectForKey:@"eventID"]integerValue];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"result"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"result"] length]==0)
        {
            result=@"";
        }
        else{
        result= [NSString stringWithFormat:@"%@",[[currentResultsList objectAtIndex:i] objectForKey:@"result"]];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"resultID"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"resultID"] length]==0)
        {
            resultID=0;
        }
        else
        {
        resultID= [[[currentResultsList objectAtIndex:i] objectForKey:@"resultID"]integerValue];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"sportID"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"sportID"] length]==0)
        {
            sportID=0;
        }
        else
        {
        sportID= [[[currentResultsList objectAtIndex:i] objectForKey:@"sportID"]integerValue];
        }
        if ([[currentResultsList objectAtIndex:i] objectForKey:@"typeOfMedal"]==(id)[NSNull null] || [[[currentResultsList objectAtIndex:i] objectForKey:@"typeOfMedal"] length]==0)
        {
            typeOfMedal=0;
        }
        else{
        typeOfMedal=[[[currentResultsList objectAtIndex:i] objectForKey:@"typeOfMedal"]integerValue];
        }
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO Result (`athleteID`,`countryMedal`,`eventID`,`result`,`resultID`,`sportID`,`typeOfMedal`) VALUES ('%d','%d', '%d','%@','%d','%d','%d')",athleteID,countryMedal,eventID,result,resultID,sportID,typeOfMedal];
        NSLog(@"InsertQery: %@", insertSQL);

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(sql, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Data Inserted");
        }
        else
        {
            NSLog(@"Data Inserted failed");        
        }

        sqlite3_finalize(statement);
        sqlite3_close(sql);
    }
}

}

0 个答案:

没有答案