IO中只有一行从SQLite返回?

时间:2014-07-18 14:43:26

标签: ios objective-c sqlite

我试图从我的SQLite中提取行,出于某种原因,只有第一行被拔出。这是我的代码:

-(void)pullNewDataFromSqlite {

    NSLog(@"pulling new data");

    NSString *fetch_sql = [NSString stringWithFormat:@"SELECT * FROM %@", tableName];


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *my_sqlfile = [[paths objectAtIndex:0] stringByAppendingPathComponent:databaseName];


    if(sqlite3_open([my_sqlfile UTF8String], &my_dbname) == SQLITE_OK) {
        NSLog(@"pulling new data, %@", fetch_sql);
    sqlite3_stmt *statement;
    NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(my_dbname));


    if(sqlite3_prepare_v2(my_dbname, [fetch_sql UTF8String], -1, &statement, nil) == SQLITE_OK) {


        int numberOfColumns = sqlite3_column_count(statement);
        //sqlite3_


        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSLog(@"NEW SQLITE ROW");

            int sdcount = sqlite3_column_int(statement, 0);
            NSLog(@"NEW SQLITE ROW number, %d", sdcount);


            for(int a = 1; a < numberOfColumns; a++) {

                char *field = (char *)sqlite3_column_text(statement, a);


                NSString *fieldVal = [[NSString alloc]initWithUTF8String:field];
                NSString *formatout = [NSString stringWithFormat:@"%@ ", fieldVal];


                //match first column with first array

                //adding each column info to the array in order

                //user id
                if(a == 1) {
                    NSLog(@"a is 1 and the value of the user id is %@", formatout);
                    [userIdsArray addObject:formatout];
                }

                //name
                if(a == 2) {
                    NSLog(@"a is 2 and the value of the name id is %@", formatout);
                    [namesArray addObject:formatout];
                }

                //picture
                if(a == 3) {
                    NSLog(@"a is 3 and the value of the pic id is %@", formatout);
                    [picturesArray addObject:formatout];
                }




            }
            //"call sqlite3_finalize to clean up the memory used for the statement, then we return the data."
            sqlite3_finalize(statement);

        }

    }
        }

    [self closeDB];

    if(count < [userIdsArray count]) {
        grabFollowers = YES;
    }

    count = [userIdsArray count];

    [self.tableView reloadData];

}

它也会显示其他2行但由于某种原因它显示为空白。

2 个答案:

答案 0 :(得分:3)

您正在sqlite3_finalize循环中调用while

在遍历所有行之前,不得调用它。

答案 1 :(得分:0)

将while(sqlite3_step(statement)== SQLITE_ROW)更改为while(sqlite3_step(statement)&lt; = SQLITE_ROW)