-(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]);
}
}
问问题
137 次
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 回答