-2
-(void) getAllRowsFromTableNamed:(NSString *)  tableName
{   
AppDelegate *app =[[UIApplication sharedApplication]delegate];
strNam = app.strName;
NSString *qsql = [ NSString stringWithFormat:@"SELECT * FROM %@", tableName];
sqlite3_stmt  *statement;
if( sqlite3_prepare_v2(db2, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK)
{    
while (sqlite3_step(statement)  ==  SQLITE_ROW)
{   
char *field1= (char *) sqlite3_column_text(statement, 0);
 NSString   *field1Str = [[NSString   alloc] initWithUTF8String:field1];
char *field2= (char *) sqlite3_column_text(statement, 1);
NSString   *field2Str = [[NSString   alloc] initWithUTF8String:field2];
 char *field3= (char *) sqlite3_column_text(statement, 2);
NSString   *field3Str = [[NSString   alloc] initWithUTF8String:field3];
char *field4= (char *) sqlite3_column_text(statement, 3);
 NSString   *field4Str = [[NSString   alloc] initWithUTF8String:field4];
NSString   *str =[[NSString alloc] initWithFormat:@"%@ - %@- %@- %@", field1Str, field2Str, field3Str, field4Str];
NSLog(@"%@",str);
 NSLog(@"%@",field1Str);

 self.strDat = field1Str;
 NSLog(@"%@", strDat);    
 array2 =[[NSMutableArray alloc]init];
 [self.array2 addObject:self.strDat]; 
 }
sqlite3_finalize(statement);
NSLog(@"%@",[array2 description]);
} 
}
4

2 に答える 2

0

配列の割り当てを「while」から外すか、挿入するたびにarray2を作成します

if( sqlite3_prepare_v2(db2, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK)

{

//here

    array2 =[[NSMutableArray alloc]init];

    while (sqlite3_step(statement) == SQLITE_ROW)

    {
        ....
        ....
        ....


        [array2 addObject:self.strDat];

    }
}
于 2012-05-16T07:09:09.527 に答える
0

このようにループの前に配列を初期化します

-(void) getAllRowsFromTableNamed:(NSString *)  tableName
{   
AppDelegate *app =[[UIApplication sharedApplication]delegate];
strNam = app.strName;
NSString *qsql = [ NSString stringWithFormat:@"SELECT * FROM %@", tableName];
sqlite3_stmt  *statement;
if( sqlite3_prepare_v2(db2, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK)
{ 
 array2 =[[NSMutableArray alloc]init];

while (sqlite3_step(statement)  ==  SQLITE_ROW)
{   
char *field1= (char *) sqlite3_column_text(statement, 0);
 NSString   *field1Str = [[NSString   alloc] initWithUTF8String:field1];
char *field2= (char *) sqlite3_column_text(statement, 1);
NSString   *field2Str = [[NSString   alloc] initWithUTF8String:field2];
 char *field3= (char *) sqlite3_column_text(statement, 2);
NSString   *field3Str = [[NSString   alloc] initWithUTF8String:field3];
char *field4= (char *) sqlite3_column_text(statement, 3);
 NSString   *field4Str = [[NSString   alloc] initWithUTF8String:field4];
NSString   *str =[[NSString alloc] initWithFormat:@"%@ - %@- %@- %@", field1Str, field2Str, field3Str, field4Str];
NSLog(@"%@",str);
 NSLog(@"%@",field1Str);

 self.strDat = field1Str;
 NSLog(@"%@", strDat);    
 [self.array2 addObject:self.strDat]; 
 }
sqlite3_finalize(statement);
NSLog(@"%@",[array2 description]);
} 
}
于 2012-05-16T07:09:14.303 に答える